因为我对自己的代码不满意。我决定删除并重写。奇怪的是,为了检查数据是否收集,我试图打印结果。结果显示不正常。我得到了这个错误。一方面,它是清晰的,但我不知道如何修复它。
错误:参数类型'Future
你能给点建议吗?多谢。
class Context {
final int id;
final String name;
Context({
this.id,
this.name,
});
}
MultiSelectBottomSheetField<Context>(
key: _multiSelectKeyContext,
initialChildSize: 0.7,
maxChildSize: 0.95,
title: Text("Context",style: TextStyle(fontSize: 19), ),
buttonText: Text("Context 66",style: TextStyle(fontSize: 19),),
searchTextStyle: TextStyle(fontSize: 19),
searchHintStyle: TextStyle(fontSize: 39),
itemsTextStyle: TextStyle(fontSize: 19),
items: getAllContextInFirebase(),
searchable: true,
onConfirm: (valueContext) {
setState(() {
_selectedContext = valueContext;
});
_multiSelectKeyContext.currentState.validate();
},
chipDisplay: MultiSelectChipDisplay(
onTap: (item) {
setState(() {
_selectedContext.remove(item);
});
_multiSelectKeyContext.currentState.validate();
},
),
),
Future<List<MultiSelectItem<Context>>>getAllContextInFirebase () async {
FirebaseFirestore.instance
.collection('users')
.doc(FirebaseAuth.instance.currentUser.uid)
.collection('contexts')
.get()
.then((QuerySnapshot querySnapshot) {
querySnapshot.docs.forEach((doc) {
print ('result query') ;
print(querySnapshot.docs.first['context_Name']);
return querySnapshot.docs.first['context_Name'];
});
});
//I have also tried the code below:
QuerySnapshot querySnapshot =
await FirebaseFirestore.instance
.collection('users')
.doc(FirebaseAuth.instance.currentUser.uid)
.collection("contexts").get();
for (int i = 0; i < querySnapshot.docs.length; i++) {
var a = querySnapshot.docs[i];
print(a.id);
}
这个问题的错误很明显:
type 'String' is not a subtype of type 'MultiSelectItem<Context>'
列表_itemsTest
是MultiSelectItem<Context>
类型的列表,您试图将snap['field_Name']
添加到其中,即String
,因此您必须找到将snap['field_Name']
转换为MultiSelectItem<Context>
类型的方法