如何知道是否有新資料添加到 Firebase 集合中。
我的問題是當新資料添加到 firebase 集合時我需要推送通知。這是我的代碼的樣子。我知道如果我把這段代碼放到我創建firebase集合的函式中它會起作用。但在這種情況下,我想在這里撰寫代碼。我怎么做 。這是我嘗試過的代碼
StreamBuilder<List<StudentNotificationModel>>(
stream: _notificationImplementaion.readNotification(),
builder: (context, snapshot) {
final notification = snapshot.data;
if (snapshot.hasError) {
return const MessageWidget('Please Try Again');
}
if (snapshot.hasData) {
if (snapshot.data == null || snapshot.data!.isEmpty) {
return Text('empty')
}
// what should i check here?
if (newdata.added) {
log('New Data added');
pushNotificationCode();
}
return Expanded(
child: ListView.builder(
physics: BouncingScrollPhysics(),
shrinkWrap: true,
itemCount: notification.length,
itemBuilder: (context, index) {
final data = notification[index];
return HomeTile(
subtitle: data.notificationType,
title: data.title,
accountType: accountType,
);
},
),
);
}
return const Loading();
});
我該怎么做呢
這個問題的解決方案
uj5u.com熱心網友回復:
您可以通過以下方式訪問自上次快照以來的檔案更改:
snapshot.data.docChanges
這將為您提供一個List<DocumentChange<Object?>>,您可以檢查串列中的每個專案,并確定更改的型別,例如第一個的型別將是:
snapshot.data.docChanges[0].type
您需要將上述值與以下值之一進行比較,每個值代表相應的變化:
DocumentChangeType.added
DocumentChangeType.modified
DocumentChangeType.removed
把它放在一起,在確定snapshot.hasData是之后true,你可以做這樣的事情:
for (var change in snapshot.data!.docChanges) {
if (change.type == DocumentChangeType.added) {
// do your logic here
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/527191.html
標籤:扑镖颤振通知
