我想在我的 Firestore 查詢中使用 where 子句,引數中有一個串列。我使用 for 回圈來執行此操作,但是當我想將所有值添加到變數中時,add 函式出現了一些問題(變數型別問題)。我理解這個問題,但我不知道我必須采取什么方式才能以另一種方式做到這一點......有人可以幫助我嗎?最好的問候:) ###這是我在堆疊上的第一個問題!:D ###
在我的主要課程中:
var games = [
"uno",
"poker",
];
return StreamProvider<List<AppUserData>>.value(
initialData: [],
value: DatabaseService().users4(games),
child: Scaffold(....
在我的資料庫類中
List<AppUserData> _list = [];
void addToList(AppUserData value) {
_list.add(value);
}
Stream<List<AppUserData>> users4 (List games) {
print(games);
var b;
var len = games.length;
for (var i = 0; i < len 1; i ) {
b = FirebaseFirestore.instance.collection("users")
.where("games", isEqualTo: games[i]).snapshots().map(
_userListFromSnapshot);
print("$b");
addToList(b);
}
return b;
}
List<AppUserData> _userListFromSnapshot(
QuerySnapshot<Map<String, dynamic>> snapshot) {
return snapshot.docs.map((doc) {
return _userFromSnapshot(doc);
}).toList();
}
AppUserData _userFromSnapshot(DocumentSnapshot<Map<String, dynamic>> snapshot) {
var data = snapshot.data();
if (data == null) throw Exception("user not found");
return AppUserData(
uid: uid,
name: snapshot.data()['name'],
distance: snapshot.data()['distance'],
games: snapshot.data()['games'],
id: snapshot.data()['id'],
);
}
uj5u.com熱心網友回復:
我想你正在尋找 whereIn
試試這個:
Stream<List<AppUserData>> users4(List games) async* {
yield* FirebaseFirestore.instance
.collection("users")
.where("games", whereIn: games)
.snapshots()
.map((e) {
List<AppUserData> l =
e.docs.map((e) => _userFromMap(Map.from(e.data()))).toList();
return l;
});
}
AppUserData _userFromMap(Map<String, dynamic> map) {
if (map == null) throw Exception("user not found");
return AppUserData(
uid: uid,
name: map['name'],
distance: map['distance'],
games: map['games'],
id: map['id'],
);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/420291.html
標籤:
