[英]Create multiple Firebase streams with custom model/class in Flutter
我的主要 function 中有一個 MultiProvider。
我想為 firebase 中的不同集合實現第二個 stream。 主要的多提供者:
StreamProvider<QuerySnapshot>(
create: (_) => getWeeksStreamSnapshot(_), //collection weeks
),
StreamProvider<QuerySnapshot>(
create: (_) => getMessagesStreamSnapshot(_), //collection messages
),
但是現在我收到一個錯誤,因為兩個流都有 QuerySnapshot 類型。
我正在使用自定義 class 尋找類似的東西:
StreamProvider<QuerySnapshot>(
create: (_) => getWeeksStreamSnapshot(_), //collection weeks
),
StreamProvider<MYCLASS>(
create: (_) => getMessagesStreamSnapshot(_), //collection messages
),
我試過這個:
Stream<MYCLASS> getMessagesStreamSnapshot(BuildContext context) async* {
try {
final auth = Provider.of<AuthenticationService>(context, listen: false);
final uid = await auth.getFormattedUID();
print("Getting messages as stream in main...");
MYCLASS(
snapshot: FirebaseFirestore.instance
.collection('users')
.doc(uid)
.collection('chat')
.snapshots()); //This is the error because .snapshots() is a stream
} catch (error) {
print(error);
}
}
有了這個 class:
class MYCLASS {
QuerySnapshot snapshot;
MYCLASS({this.snapshot});
}
如何使 MYCLASS class 返回 QuerySnapshot,所以當我訪問Provider.of<MYCLASS>(context)
時很清楚
Firebase 看起來像這樣:
所以我正在尋找的文檔稱為消息(在聊天集合中)。
那這個呢:
getMessagesStreamSnapshot() async* {
var querySnapshot;
const sec = Duration(seconds: 1);
Timer.periodic(sec, (Timer t) async {
querySnapshot = await FirebaseFirestore.instance
.collection('users')
.doc(uid)
.collection('chat')
.get();
});
yield { 'snapshot':querySnapshot,'type':'message'};
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.