为什么我会在flutter

时间:2019-11-14 15:46:35

标签: firebase flutter dart google-cloud-firestore

因此,我有一个通知屏幕和一个 MediaPreview (用于在通知旁边显示图片)。 并且在媒体预览上(点击)时,我想重定向到另一个页面以显示图像,由于某种原因,它给了我方法[]为空

这是我点击图片时显示“发布”的代码

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

这是PostScreen的代码:

class PostScreen extends StatelessWidget {
  final String userId;
  final String postId;

  PostScreen({this.postId, this.userId});

  @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, appTitle: post.description),
            body: ListView(
              children: <Widget>[
                Container(
                  child: post,
                )
              ],
            ),
          ),
        );
      },
    );
  }
}

1 个答案:

答案 0 :(得分:0)

因此我在Post post = Post.fromDocument(snapshot.data);中使用了调试器和断点后,找到了解决方案,我发现在 postId 中找不到一个userId 帖子集合(我们称其为用户A),因此用户 A 没有具有此 postId 的帖子,但用户 B < / strong> 一直在寻找帖子。因此,我从:

更改了showPost函数。

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

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

以这种方式,当我单击另一个 user 帖子时(我获得了 userId 以获取他的 posts帖子),并且当我单击< strong>我的帖子(我获得我的用户ID 以获得我的帖子集合)。

希望这会有所帮助。