【问题标题】:Flutter Firestore query to return listFlutter Firestore 查询返回列表
【发布时间】:2018-11-22 04:44:06
【问题描述】:

我正在使用 Flutter 和 Firebase 构建一个类似社交网络的程序,目前需要找到返回用户未关注的用户列表。

为了实现这一点,我正在尝试生成用户正在关注的用户 ID 列表,然后循环遍历每个用户 ID(可能会设置限制)并确定该 ID 是否在用户 ID 列表中用户没有关注。然后我会为此返回一个小部件列表。

目前,我正在尝试调用当前关注者的 Stream 以返回用户当前关注的每个人的列表,但 Flutter 不允许我从中返回列表?

class AddFollowers extends StatelessWidget {
  final String userId = 'userID imported here';

  Stream<List<String>> getFollowers(userId) async {
    final QuerySnapshot result = await Firestore.instance
        .collection('relationships')
        .document(userId)
        .collection('followers')
        .where("follower", isEqualTo: true)
        .getDocuments();
    final List<DocumentSnapshot> documents = result.documents;
    return documents;
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: AppBar(
        elevation: 0.0,
        leading: IconButton(
          icon: Icon(Icons.close),
          onPressed: () => Navigator.of(context).pop(),
        ),
        title: new Text("Find Your Following"),
      ),
      body: new Container(
        child: new StreamBuilder(
            stream: getFollowers(userId),
            builder:
                null //after returning the followers, I will build widgets of non-followers
            ),
      ),
//        new FindFollowerWidget(),
    );
  }
}

非常感谢任何关于如何返回这个或更好的结构的建议。谢谢!

【问题讨论】:

  • .getDocuments();返回 Future 而不是 Stream。你应该简单地做 - Firestore.instance .collection('relationships') .document(userId) .collection('followers') .where("follower", isEqualTo: true) .snapshots();返回结果;
  • 谢谢!那行得通,是我的问题!如果您留下作为答案,很乐意接受!

标签: flutter google-cloud-firestore


【解决方案1】:

由于您使用的是Stream,因此我假设您希望收听您的收藏和文档的实时更改。 FlutterFire 提供 CollectionReference 和 DocumentReference 两者都有 snapshots() 方法,该方法返回您可以使用的 Stream。

对于您的用例,您可以像这样使用它:

// either set the collection as a Stream
Stream getCollectionStream(String userId){
  return FirebaseFirestore.instance
        .collection('relationships')
        .document(userId)
        .collection('followers')
        .where("follower", isEqualTo: true)
        .snapshots();
}

// or set it as a CollectionReference
CollectionReference getCollectionReference(String userId){
  // this returns a CollectionReference  
  return FirebaseFirestore.instance
        .collection('relationships')
        .document(userId)
        .collection('followers')
        .where("follower", isEqualTo: true);
}

在你的Widget build()

@override
Widget build(BuildContext context) {
  return new Scaffold(
    ...
    body: new Container(
      child: new StreamBuilder(
        // either this Stream snapshot
        stream: getCollectionStream(userId),
        // or use the reference with snapshots() method for the Stream
        // stream: getCollectionReference(userId).snapshots(),
        builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
          if (snapshot.hasError) {
            return Text('Something went wrong');
          }

          if (snapshot.connectionState == ConnectionState.waiting) {
            // display progressBar
            return Text("Loading");
          }
        ),
      },
    ),
  );
}

有关在 Cloud Firestore 上侦听实时更改的更多详细信息,请参阅guide。

【讨论】:

    猜你喜欢
    • 2021-08-12
    • 2020-11-04
    • 2021-12-27
    • 2021-02-20
    • 1970-01-01
    • 1970-01-01
    • 2020-10-22
    • 2019-11-02
    • 2023-03-09
    相关资源
    最近更新 更多