【问题标题】:The method '[]' was called on null. Flutter在 null 上调用了方法“[]”。扑
【发布时间】:2019-12-21 15:23:48
【问题描述】:

这是 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,
      ),
    ),
  );
}

遇到错误

在 null 上调用了方法“[]”。 接收方:空 尝试调用: 相关的导致错误的小部件是: FutureBuilder file:///home/testflutter/AndroidStudioProjects/testingflutter/lib/pages/post_screen.dart:15:12

【问题讨论】:

    标签: flutter dart nosuchmethoderror


    【解决方案1】:

    添加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,
                    )
                  ],
                ),
              ),
            );
            } 
          },
        );
    

    【讨论】:

      【解决方案2】:

      检查快照的ConnectionState 看是否完成。

      Widget build(BuildContext context) {
        return FutureBuilder(
          future: postsRef
              .document(userId)
              .collection('userPosts')
              .document(postId)
              .get(),
          builder: (context, snapshot) {
            if (snapshot.connectionState == ConnectionState.waiting)
              return circularProgress();
            else if (snapshot.connectionState == ConnectionState.done) {
              if (snapshot.hasData && snapshot.data.isNotEmpty) {
                Post post = Post.fromDocument(snapshot.data);
                return Center(
                  child: Scaffold(
                    appBar: header(context, titleText: post.description),
                    body: ListView(
                      children: <Widget>[
                        Container(
                          child: post,
                        ),
                      ],
                    ),
                  ),
                );
              } else {
                return Center(child: Text("No data or it's empty"));
              }
            } else {
              return Center(child: Text("Neither waiting or done..."));
            }
          },
        );
      }
      

      我已经修复了代码以使其更容易,并添加了 else 子句。

      【讨论】:

        【解决方案3】:

        可能的错误是,在您的 showPost() 方法中。

        在 home.dart 文件中初始化一个用户对象。

        User currentUser;

        然后像这样更新 showPost() 方法:

        showPost(BuildContext context) {
            Navigator.push(
              context,
              MaterialPageRoute(
                builder: (context) =>
                    PostScreen(userId: currentUser.id, postId: postId),
              ),
            );
          }
        

        【讨论】:

          猜你喜欢
          • 2021-01-19
          • 2020-11-15
          • 2021-11-15
          • 2021-09-22
          • 2021-06-11
          • 2021-03-06
          • 2021-07-13
          • 2020-01-17
          • 2020-02-21
          相关资源
          最近更新 更多