Firebase 如何从带有flatter的文档的firestore数组中获取数据?
这是我在cloud fire store中的用户集合: 这是从firestore中的用户集合获取用户的函数Firebase 如何从带有flatter的文档的firestore数组中获取数据?,firebase,flutter,dart,google-cloud-firestore,Firebase,Flutter,Dart,Google Cloud Firestore,这是我在cloud fire store中的用户集合: 这是从firestore中的用户集合获取用户的函数 Stream<QuerySnapshot> fetchUsersInSearch() { return Firestore.instance.collection('users').snapshots(); } 在下面的streamBuilder中,通过电子邮件获取用户 我有这个streamBuilder来填充屏幕上的数据 return StreamBuild
Stream<QuerySnapshot> fetchUsersInSearch() {
return Firestore.instance.collection('users').snapshots();
}
在下面的streamBuilder中,通过电子邮件获取用户
我有这个streamBuilder来填充屏幕上的数据
return StreamBuilder<QuerySnapshot>(
stream: DatabaseService().fetchUsersInSearch(),
builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
final emailResults = snapshot.data.documents
.where((u) => u['email'].contains(query));
if (!snapshot.hasData) {
return Container(
color: Theme.of(context).primaryColor,
child: Center(
child: Text(
'',
style: TextStyle(
fontSize: 16, color: Theme.of(context).primaryColor),
),
),
);
}
if (emailResults.length > 0) {
return Container(
color: Theme.of(context).primaryColor,
child: ListView(
children: emailResults
.map<Widget>((u) => GestureDetector(
child: Padding(
padding: const EdgeInsets.all(0.1),
child: Container(
padding: EdgeInsets.symmetric(vertical: 5),
decoration: BoxDecoration(
color: Theme.of(context).primaryColor,
border: Border(
bottom: BorderSide(
width: 0.3, color: Colors.grey[50]))),
child: ListTile(
leading: CircleAvatar(
backgroundColor:
Theme.of(context).primaryColor,
backgroundImage:
NetworkImage(u['userAvatarUrl']),
radius: 20,
),
title: Container(
padding: EdgeInsets.only(left: 10),
child: Column(
crossAxisAlignment:
CrossAxisAlignment.start,
children: [
Text(u['email'],
style: TextStyle(
fontSize: 16,
color: Theme.of(context)
.accentColor),
overflow: TextOverflow.ellipsis),
SizedBox(
height: 5,
),
],
),
),
),
),
),
onTap: () {
showUserProfile(u['id']);
},
))
.toList(),
),
);
} else {
return Container(
color: Theme.of(context).primaryColor,
child: Center(
child: Text(
'No results found',
style: TextStyle(
fontSize: 16,
color: Theme.of(context).accentColor,
),
),
),
);
}
});
但它返回了这个错误:
The method 'contains' was called on null.
Receiver: null
Tried calling: contains("#username1")
我做错了什么
任何帮助都将不胜感激。试试以下方法:-
Stream<QuerySnapshot> getUsers() {
final usersCollection = FirebaseFirestore.instance.collection('users');
return usersCollection.where('otherUsernames', arrayContainsAny: ['username1', 'username2']);
}
Stream getUsers(){
final usersCollection=FirebaseFirestore.instance.collection('users');
返回usersCollection.where('otherUsernames',arrayContainsAny:['username1','username2']);
}
对于firestore 0.16.0版我对您的答案做了一些更正以使其生效:Stream getUsers(){final users collection=firestore.instance.collection('users').snapshots();return users collection.where('otherUsernames',arrayContainsAny:['username1','username2']);}但至少这种方法让我很高兴,我们可以通过子集合搜索用户
The method 'contains' was called on null.
Receiver: null
Tried calling: contains("#username1")
Stream<QuerySnapshot> getUsers() {
final usersCollection = FirebaseFirestore.instance.collection('users');
return usersCollection.where('otherUsernames', arrayContainsAny: ['username1', 'username2']);
}