flutter - 在空方法上调用了 '[]'方法。 flutter

标签 flutter dart nosuchmethoderror

这是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/

相关文章:

flutter - polAnd替换显示页面在Flutter的弹出页面之后

java - "Error: Main method not found in class MyClass, please define the main method as..."

flutter - 元素类型 'Question'无法分配给列表类型 'Widget'

flutter - 将 Wrap 小部件与文本一起使用

flutter - 如何在 Flutter 中创建 GridView 布局

flutter - 当我在Firestore中更新数据时,更新数据功能不起作用?

flutter - 如何测试流是否在Dart中的特定位置发出值?

json - Flutter:将整数列表转换为一个衬里中的字符串列表

java - Apache Kafka 异常 : org. apache.kafka.clients.consumer.KafkaConsumer.subscribe(Ljava/util/List;)V

java - 试图解决NoSuchMethodError