这是FutureBuilder:
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: postsRef
.document(userId)
.collection('userPosts')
.document(postId)
.get(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return circularProgress();
}
Post post = Post.fromDocument(snapshot.data);
return Center(
child: Scaffold(
appBar: header(context, titleText: post.description),
body: ListView(
children: <Widget>[
Container(
child: post,
)
],
),
),
);
},
);
}
引用它的show post方法:
showPost(context) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => PostScreen(
postId: postId,
userId: userId,
),
),
);
}
低于错误
The method '[]' was called on null. Receiver: null Tried calling: The relevant error-causing widget was: FutureBuilder file:///home/testflutter/AndroidStudioProjects/testingflutter/lib/pages/post_screen.dart:15:12
最佳答案
添加else
子句
Widget build(BuildContext context) {
return FutureBuilder(
future: postsRef
.document(userId)
.collection('userPosts')
.document(postId)
.get(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return circularProgress();
}
else {
Post post = Post.fromDocument(snapshot.data);
return Center(
child: Scaffold(
appBar: header(context, titleText: post.description),
body: ListView(
children: <Widget>[
Container(
child: post,
)
],
),
),
);
}
},
);
关于flutter - 在空方法上调用了 '[]'方法。 flutter ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59437408/