有没有办法通过flutter查看来自云Firestore的用户的关注者或关注者?
Posted
技术标签:
【中文标题】有没有办法通过flutter查看来自云Firestore的用户的关注者或关注者?【英文标题】:Is there a way to view followers or following of a user from cloud firestore with flutter? 【发布时间】:2019-11-26 17:39:48 【问题描述】:我一直在尝试查看是否可以连接至少两个集合,例如关注者和用户,以便能够查看关注用户的关注者。
我有 3 个集合,分别称为用户、关注者和关注者。
用户 -uid --用户信息
追随者 -uid --userFollowers(子集合) ---关注者ID
以下内容 -uid --userFollowing(子集合) ---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】:当您使用 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
【讨论】:
好的。嗯,设备屏幕的错误消失了,但是我还是看不到用户的关注者。以上是关于有没有办法通过flutter查看来自云Firestore的用户的关注者或关注者?的主要内容,如果未能解决你的问题,请参考以下文章
有没有办法确定用户是通过自定义 Firebase 帐户还是通过 Flutter 中的 Google 帐户登录的?