如何在 flutter 中修复 'getter "documents" was called on null.'
How to fix 'getter "documents" was called on null.' in flutter
我正在使用 flutter 和 firebase 创建一个移动应用程序。我的 firestore 上有 2 个集合,我想阅读集合 "posts" 中的所有文档。但是,当我这样做时,出现一个错误,指出 getter "documents" was called on null.
Widget getContent(BuildContext context) {
return StreamBuilder<QuerySnapshot>(
stream: Firestore.instance.collection("posts").snapshots(),
builder: (context, snap) {
return CarouselSlider(
enlargeCenterPage: true,
height: MediaQuery.of(context).size.height,
items: getItems(context, snap.data.documents),
);
},
);
}
List<Widget> getItems(BuildContext context, List<DocumentSnapshot>
docs){
return docs.map(
(doc) {
String content = doc.data["content"];
return Text(content);
}
).toList();
}
我希望在所有文档中都包含数据,但是却收到了这个错误:
I/flutter (30878): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (30878): The following NoSuchMethodError was thrown building StreamBuilder<QuerySnapshot>(dirty,
I/flutter (30878): dependencies: [MediaQuery], state: _StreamBuilderBaseState<QuerySnapshot,
I/flutter (30878): AsyncSnapshot<QuerySnapshot>>#72d38):
I/flutter (30878): The getter 'documents' was called on null.
I/flutter (30878): Receiver: null
I/flutter (30878): Tried calling: documents
I/flutter (30878): When the exception was thrown, this was the stack:
V/NativeCrypto(30878): Registering com/google/android/gms/org/conscrypt/NativeCrypto's 284 native methods...
I/flutter (30878): #0 Object.noSuchMethod (dart:core/runtime/libobject_patch.dart:50:5)
I/flutter (30878): #1 PostsPageState.getContent.<anonymous closure>
package:reach_out_kef_global/main.dart:140
I/flutter (30878): #2 StreamBuilder.build
package:flutter/…/widgets/async.dart:423
I/flutter (30878): #3 _StreamBuilderBaseState.build
package:flutter/…/widgets/async.dart:125
I/flutter (30878): #4 StatefulElement.build
package:flutter/…/widgets/framework.dart:3825
I/flutter (30878): #5 ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:3736
I/flutter (30878): #6 Element.rebuild
package:flutter/…/widgets/framework.dart:3559
I/flutter (30878): #7 ComponentElement._firstBuild
package:flutter/…/widgets/framework.dart:3716
I/flutter (30878): #8 StatefulElement._firstBuild
package:flutter/…/widgets/framework.dart:3864
I/flutter (30878): #9 ComponentElement.mount
package:flutter/…/widgets/framework.dart:3711
I/flutter (30878): #10 Element.inflateWidget
package:flutter/…/widgets/framework.dart:2956
I/flutter (30878): #11 Element.updateChild
package:flutter/…/widgets/framework.dart:2759
I/flutter (30878): #12 ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:3747
I/flutter (30878): #13 Element.rebuild
package:flutter/…/widgets/framework.dart:3559
I/flutter (30878): #14 ComponentElement._firstBuild
package:flutter/…/widgets/framework.dart:3716
I/flutter (30878): #15 StatefulElement._firstBuild
package:flutter/…/widgets/framework.dart:3864
I/flutter (30878): #16 ComponentElement.mount
package:flutter/…/widgets/framework.dart:3711
...
请帮忙!
您应该在调用 snapshot.data.documents 之前检查 snapshot.data 是否为空。这通常是我在流构建器的构建方法中做的第一件事。如果流为空,就像第一次侦听时一样,但在 firestore 返回所请求的数据之前,snapshot.data 将为空。在这种情况下,您可能希望显示一个容器或一个循环进度指示器:
Widget getContent(BuildContext context) {
return StreamBuilder<QuerySnapshot>(
stream: Firestore.instance.collection("posts").snapshots(),
builder: (context, snap) {
//just add this line
if(snap.data == null) return CircularProgressIndicator();
return CarouselSlider(
enlargeCenterPage: true,
height: MediaQuery.of(context).size.height,
items: getItems(context, snap.data.documents),
);
},
);
}
List<Widget> getItems(BuildContext context, List<DocumentSnapshot>
docs){
return docs.map(
(doc) {
String content = doc.data["content"];
return Text(content);
}
).toList();
}
我正在使用 flutter 和 firebase 创建一个移动应用程序。我的 firestore 上有 2 个集合,我想阅读集合 "posts" 中的所有文档。但是,当我这样做时,出现一个错误,指出 getter "documents" was called on null.
Widget getContent(BuildContext context) {
return StreamBuilder<QuerySnapshot>(
stream: Firestore.instance.collection("posts").snapshots(),
builder: (context, snap) {
return CarouselSlider(
enlargeCenterPage: true,
height: MediaQuery.of(context).size.height,
items: getItems(context, snap.data.documents),
);
},
);
}
List<Widget> getItems(BuildContext context, List<DocumentSnapshot>
docs){
return docs.map(
(doc) {
String content = doc.data["content"];
return Text(content);
}
).toList();
}
我希望在所有文档中都包含数据,但是却收到了这个错误:
I/flutter (30878): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (30878): The following NoSuchMethodError was thrown building StreamBuilder<QuerySnapshot>(dirty,
I/flutter (30878): dependencies: [MediaQuery], state: _StreamBuilderBaseState<QuerySnapshot,
I/flutter (30878): AsyncSnapshot<QuerySnapshot>>#72d38):
I/flutter (30878): The getter 'documents' was called on null.
I/flutter (30878): Receiver: null
I/flutter (30878): Tried calling: documents
I/flutter (30878): When the exception was thrown, this was the stack:
V/NativeCrypto(30878): Registering com/google/android/gms/org/conscrypt/NativeCrypto's 284 native methods...
I/flutter (30878): #0 Object.noSuchMethod (dart:core/runtime/libobject_patch.dart:50:5)
I/flutter (30878): #1 PostsPageState.getContent.<anonymous closure>
package:reach_out_kef_global/main.dart:140
I/flutter (30878): #2 StreamBuilder.build
package:flutter/…/widgets/async.dart:423
I/flutter (30878): #3 _StreamBuilderBaseState.build
package:flutter/…/widgets/async.dart:125
I/flutter (30878): #4 StatefulElement.build
package:flutter/…/widgets/framework.dart:3825
I/flutter (30878): #5 ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:3736
I/flutter (30878): #6 Element.rebuild
package:flutter/…/widgets/framework.dart:3559
I/flutter (30878): #7 ComponentElement._firstBuild
package:flutter/…/widgets/framework.dart:3716
I/flutter (30878): #8 StatefulElement._firstBuild
package:flutter/…/widgets/framework.dart:3864
I/flutter (30878): #9 ComponentElement.mount
package:flutter/…/widgets/framework.dart:3711
I/flutter (30878): #10 Element.inflateWidget
package:flutter/…/widgets/framework.dart:2956
I/flutter (30878): #11 Element.updateChild
package:flutter/…/widgets/framework.dart:2759
I/flutter (30878): #12 ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:3747
I/flutter (30878): #13 Element.rebuild
package:flutter/…/widgets/framework.dart:3559
I/flutter (30878): #14 ComponentElement._firstBuild
package:flutter/…/widgets/framework.dart:3716
I/flutter (30878): #15 StatefulElement._firstBuild
package:flutter/…/widgets/framework.dart:3864
I/flutter (30878): #16 ComponentElement.mount
package:flutter/…/widgets/framework.dart:3711
...
请帮忙!
您应该在调用 snapshot.data.documents 之前检查 snapshot.data 是否为空。这通常是我在流构建器的构建方法中做的第一件事。如果流为空,就像第一次侦听时一样,但在 firestore 返回所请求的数据之前,snapshot.data 将为空。在这种情况下,您可能希望显示一个容器或一个循环进度指示器:
Widget getContent(BuildContext context) {
return StreamBuilder<QuerySnapshot>(
stream: Firestore.instance.collection("posts").snapshots(),
builder: (context, snap) {
//just add this line
if(snap.data == null) return CircularProgressIndicator();
return CarouselSlider(
enlargeCenterPage: true,
height: MediaQuery.of(context).size.height,
items: getItems(context, snap.data.documents),
);
},
);
}
List<Widget> getItems(BuildContext context, List<DocumentSnapshot>
docs){
return docs.map(
(doc) {
String content = doc.data["content"];
return Text(content);
}
).toList();
}