Flutter Firebase:StreamBuilder不听更改吗?

时间:2018-10-27 22:11:24

标签: firebase flutter

我正在尝试创建一个UI,其中用户在每个“文章”上提交一个“评论”。如下所示,在我的代码中,我从Firestore获取该用户的用户名,然后检查该用户是否已对相应文章发表评论。

如果评论存在,则返回给定文章的所有评论流。如果它不存在,我将返回CommentCollector,它是一个小部件,用于收集评论并将其发布到Firestore。总体而言,这种效果很好,希望当我通过CommentCollector提交评论时,UserPostGetter小部件不会重新构建。

如何触发此重建?我认为使用StreamBuilder来听是否有评论就足够了,但显然不是。我想念什么?

非常感谢您的帮助。

class UserPostGetter extends StatelessWidget {
  final String articleId;
  final String articleHeader;

  UserPostGetter({this.articleId, this.articleHeader});

  @override
  Widget build(BuildContext context) {
// TODO: implement build
    return new Scaffold(
      body: new Container(
        child: new FutureBuilder<FirebaseUser>(
          future: FirebaseAuth.instance.currentUser(),
          builder: (context, AsyncSnapshot<FirebaseUser> snapshot) {
            if (snapshot.connectionState == ConnectionState.done) {
              String userNumber = snapshot.data.uid;
              return new FutureBuilder(
                future: getUser(userNumber),
                builder: (context, AsyncSnapshot<User> snapshot) {
                  if (snapshot?.data == null)
                    return new Center(
                      child: new Text("Loading..."),
                    );
                  String username = snapshot.data.username.toString();
                  return StreamBuilder(
                    stream: doesNameAlreadyExist(articleId, username),
                    builder: (context, AsyncSnapshot<bool> result) {
                      if (!result.hasData)
                        return Container(); // future still needs to be finished (loading)
                      if (result
                          .data) // result.data is the returned bool from doesNameAlreadyExists
                        return PostGetter(
                          articleId: articleId,
                        );
                      else
                        return CommentCollector(
                          articleID: articleId,
                          userName: username,
                          articleTitle: articleHeader,
                        );
                    },
                  );
                },
              );
            } else {
              return new Text('Loading...');
            }
          },
        ),
      ),
    );
  }
}

Stream<bool> doesNameAlreadyExist(String article, String name) async* {
  final QuerySnapshot result = await Firestore.instance
      .collection('post')
      .where('article', isEqualTo: article)
      .where('author', isEqualTo: name)
      .getDocuments();
  final List<DocumentSnapshot> documents = result.documents;
  yield documents.length == 1;
}

Future<User> getUser(idNumber) {
  return Firestore.instance
      .collection('user')
      .document('$idNumber')
      .get()
      .then((snapshot) {
    try {
      return User.fromSnapshot(snapshot);
    } catch (e) {
      print(e);
      return null;
    }
  });
}

class User {
  String email;
  String user_id;
  String username;
  User.fromSnapshot(DocumentSnapshot snapshot)
      : email = snapshot['email'],
        user_id = snapshot['user_id'],
        username = snapshot['username'];
}

0 个答案:

没有答案