[英]Flutter: StreamBuilder into ListView doubling list items when adding to it
[英]Firebase Flutter StreamBuilder when dismiss the list from listview the Screen is lagging a bit
鏈接是我的情況 in.mp4 3 secs the lagging screen 。 在我關閉列表視圖並更新文檔后,我發現我的屏幕顯示了一些滯后情況。
這是我可忽略的小部件。
Dismissible(
direction: DismissDirection.endToStart,
background: Container(
color: Colors.red,
),
onDismissed: (DismissDirection direction) {
setState(
() {
healthGoal.removeAt(index);
healthgoalCollection
.doc("c66ae3ef-9853-4")
.update(
{
"healthGoalList": healthGoal,
},
);
},
);
},
這是我的 stream 構建器小部件,我從中獲取數據
StreamBuilder<DocumentSnapshot>(
stream: _healthGoalStream,
builder: (context, AsyncSnapshot<DocumentSnapshot> snapshot) {
if (!snapshot.hasData) {
return const Text("No data");
}
if (snapshot.hasError) {
return const Center(child: Text('Something went wrong'));
}
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(
child: CircularProgressIndicator(
color: Colors.red,
),
);
}
if (snapshot.connectionState == ConnectionState.active) {
Map<String, dynamic> condition =
snapshot.data?.data() as Map<String, dynamic>;
Map<String, dynamic> conditionmap =
Map<String, dynamic>.from(condition);
healthGoal.clear();
conditionmap.forEach(
(key, value) {
if (key == "healthGoalList") {
healthGoal = value;
}
if (key == "nameUpdated") {
if (value != "") {
nameupdate = value;
}
}
if (key == "agreeToGoal") {
if (value != "") {
agreeToGoal = value;
}
}
if (key == "dateTimeUpdated") {
if (value != "") {
timeget = DateTime.fromMillisecondsSinceEpoch(value);
timeupdate = f.format(timeget);
}
}
},
);
有什么方法可以防止這種滯后的情況嗎? 謝謝你能幫助我,甚至給我一個關於事業的方向。
這是我的雲存儲數據看起來像
2 我可以從您的代碼中注意到的事情。
調用set state
是不必要的,因為您正在使用 StreamBuilder 實時監聽 firebase 上的更改。 如果有任何傳入的更改,將再次調用 builder 方法。 閱讀更多關於 firebase 實時火庫 https://firebase.flutter.dev/docs/firestore/usage/#realtime-changes
使用鍵來防止小部件重建。 更多關於這里https://api.flutter.dev/flutter/foundation/Key-class.html 。
Dismissible(key: ValueKey(healthGoal[index]) );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.