有没有办法查看扑朔迷离的Cloud Firestore中的关注者或关注者?

时间:2019-11-26 17:39:48

标签: flutter google-cloud-firestore

我一直在尝试查看是否可以连接至少两个集合(例如关注者和用户),以便查看正在关注该用户的关注者。

我有3个收藏集,分别称为用户,关注者和关注者。

  

个用户   -uid   -用户信息

     

追随者   -uid   --userFollowers(子集合)   --- followerID

     

关注   -uid   --user关注(子集合)   --- followingID

这是我的代码:

class Followers extends StatefulWidget {
  final String uid;
  final String currentUID;
  Followers({this.currentUID, this.uid});

  @override
  _FollowersState createState() => _FollowersState();
}

class _FollowersState extends State<Followers> {
  Future<QuerySnapshot> _followers;
  TextEditingController _searchController = TextEditingController();

  _clearSearch() {
    WidgetsBinding.instance
        .addPostFrameCallback((_) => _searchController.clear());
    setState(() {
      _followers = null;
    });
  }

  @override
  Widget build(BuildContext context) {
    return StreamBuilder(
      stream: followersRef.document(widget.uid).collection('userFollowers').snapshots(),
      builder: (context, follower) {
        Follower followers = Follower.fromDoc(doc: follower.data);
        return Scaffold(
          appBar: AppBar(
            backgroundColor: Colors.white,
            title: TextField(
              controller: _searchController,
              decoration: InputDecoration(
                contentPadding: EdgeInsets.symmetric(vertical: 15.0),
                border: InputBorder.none,
                hintText: 'Search',
                prefixIcon: Icon(
                  Icons.search,
                  size: 30.0,
                ),
                suffixIcon: IconButton(
                  icon: Icon(
                    Icons.clear,
                  ),
                  onPressed: _clearSearch,
                ),
                filled: true,
              ),
              onSubmitted: (input) {
                if (input.isNotEmpty) {
                  setState(() {
                    _followers = UserDatabase.searchUsers(input);
                  });
                }
              },
            ),
          ),
          body:  FutureBuilder(
            future: _followers,
            builder: (context, snapshot) {
              if (!snapshot.hasData) {
                return Center(
                  child: CircularProgressIndicator(),
                );
              }
              if (snapshot.data.documents.length == 0) {
                return Center(
                  child: Text('No users found! Please try again.'),
                );
              }
              return ListView.builder(
                itemCount: snapshot.data.documents.length,
                itemBuilder: (BuildContext context, int index) {
                  User user = User.fromDocument(doc: snapshot.data.documents[index]);
                  return _displayFollowers(user: user, followerID: followers.followerID);
                },
              );
            },
          ),
        );
      }
    );
  }

  _displayFollowers({String followerID, User user}) {
    return followerID == user.userId ? ListTile(
        ....
      ),
    ) : Container();
  }
}

这基本上是针对followerID的

class Follower {
  final String followerID;
  Follower({this.followerID});

  factory Follower.fromDoc({DocumentSnapshot doc}) => Follower(
    followerID: doc.documentID
  );
}

我在设备屏幕上显示的错误

  

类型'QuerySnapshot'不是重复时类型'DocumentSanpshot'的子类型

调试控制台上的错误

The following NoSuchMethodError was thrown building StreamBuilder<QuerySnapshot>(dirty, state: _StreamBuilderBaseState<QuerySnapshot, AsyncSnapshot<QuerySnapshot>>#52445):
The getter 'documentID' was called on null.
Receiver: null
Tried calling: documentID
When the exception was thrown, this was the stack
0      Object.noSuchMethod  (dart:core-patch/object_patch.dart:50:5)
1      new Follower.fromDoc 
package:practice/UserProfile/followers.dart:120
2      _FollowersState.build.<anonymous closure> 
package:practice/UserProfile/followers.dart:36
3      StreamBuilder.build 
package:flutter/…/widgets/async.dart:425
4      _StreamBuilderBaseState.build 
package:flutter/…/widgets/async.dart:125
...

无论如何,我是否可以解决问题并能够为用户显示关注者列表或关注者列表?请,谢谢你

1 个答案:

答案 0 :(得分:0)

使用StreamBuilder或FutureBuilder时,需要通过检查snapshopt.connectionState来检查StreamBuilder,以检查快照是否已完成。

return StreamBuilder(
  stream: followersRef.document(widget.uid).collection('userFollowers').snapshots(),
  builder: (context, follower) {
    if (follower.connectionState == ConnectionState.done) {
      // You can  use follower.data
    } else {
      // Waiting for data / errors / etc
    }
  }