我的 UI 上有 3 個可選專案。我用ListViewBuilder. 我的目標是將selectedItem's 上傳到 Firebase。無論如何,例如;
List<Map<String?, dynamic>> selectedItems = List.empty(growable: true);
在上傳 Firebase 之前,串列將是這樣的:
List selectedItems = [
{
'title': selectedItem[index].title,
'price': selectedItem[index].price,
},
];
這必須是我的selectedItems清單。
我Map像這樣上傳這個串列:
Map<String, dynamic> updatedData = {'items': selectedItems};
為了 Firebase 設計,這必須是這樣的。
首先,當用戶單擊我要檢查的專案時selectedItems。這是我的代碼,但它根本不起作用:
if (selectedServices.contains(
[{
'title': selectedItem[index].title,
'price': selectedItem[index].price,
}])
問題就在這里。在此檢查之后(由于不作業,它總是通過)我嘗試selectedItems使用以下代碼洗掉相同的專案:
selectedServices.removeWhere((item) => mapEquals(
item,
({
'title': selectedItem[index].title,
'price': selectedItem[index].price,
})));
但它也不起作用,因為檢查總是通過假。在這些步驟之后,我的 onPressed 函式只在這個else代碼塊上作業。
else
{
selectedServices.addAll([{
selectedItem[index].title:
selectedItem[index].price
}
]);}
A little summary;
.contains method not working like this. If there is another way to set list of maps, just let me learn that. This is important for me but I can't figure it out last few days.
And lastly, can you explain answer how it works for Dart ?
uj5u.com熱心網友回復:
編輯:問題的焦點改變了,所以這個答案現在試圖contains為具有復雜專案的串列實作類似的功能。
contains對于自定義相等檢查,可以通過以下方式實作類似的功能any:
import 'package:collection/equality.dart';
void main() {
final map1 = {
'title': 'item1',
'price': 12.34,
};
final map2 = {
'title': 'item2',
'price': 3.45,
};
final list = [map1, map2];
final test1 = list.any((item)=>MapEquality().equals(item, {
'title': 'item1',
'price': 12.34,
}));
print(test1);
final test2 = list.any((item)=>MapEquality().equals(item, {
'title': 'item3',
'price': 14.34,
}));
print(test2);
}
列印是:
true
false
但是,您可能會遇到雙精度問題,或者如果您在地圖中使用復雜的鍵值對。
uj5u.com熱心網友回復:
您不要嘗試在此代碼中檢查陣列是否包含其他陣列:
if (selectedServices.contains(
[{
'title': selectedItem[index].title,
'price': selectedItem[index].price,
}])
?
嘗試檢查引數串列中的一項:
if (selectedServices.contains(
{'title': selectedItem[index].title}
))
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/447446.html
標籤:list firebase flutter dictionary listview
上一篇:WPF轉換器:無法將“System.Int32”型別的物件轉換為“System.Windows.Controls.ListViewItem”型別
