当作为参数传递时,List 内的元素字符串变为空。它以前工作过。我不知道发生了什么,它停止工作,并开始空着传递。
我有一个名为 SubjectiveList 的模型,它是我正在谈论的列表。
class SubjectiveList {
String id;
String name;
List<Item> items;
SubjectiveList({this.id, this.name, this.items});
}
此列表具有属性项。空的是 Item 对象内的属性。
class Item {
String id;
String name;
Content content;
Item({this.id, this.name, this.content});
}
在调试器上, newList 实例看起来很好,带有对象名称(ps:此时 ID 可以为空,因为它稍后将来自 Firestore 数据库)
Future<dynamic> showListInfoDialog() {
final userData = Provider.of<UserData>(context, listen: false);
GlobalKey<FormState> _addListInfoFormKey = GlobalKey<FormState>();
final ValueNotifier<int> tabIndex =
Provider.of<ValueNotifier<int>>(context, listen: false);
TempListViewModel tempList =
Provider.of<TempListViewModel>(context, listen: false);
return showDialog(
context: context,
child: SimpleDialog(
title: Text("List Info"),
children: <Widget>[
Padding(
padding: const EdgeInsets.all(defaultSpacing),
child: Form(
key: _addListInfoFormKey,
child: Column(
children: <Widget>[
TextFormField(
onChanged: (val) => tempList.setListName(val),
validator: (val) => val.isEmpty ? 'Write a name' : null,
decoration: InputDecoration(
prefixIcon: Icon(Icons.featured_play_list),
labelText: "List Name",
),
),
SizedBox(height: defaultSpacing),
SizedBox(
width: double.infinity,
child: RaisedButton(
child: Text("Create List"),
color: successColor,
onPressed: () {
if (_addListInfoFormKey.currentState.validate()) {
final newList = SubjectiveList(
name: tempList.list.name,
items: tempList.list.items);
DatabaseService(uid: userData.uid)
.addListToDatabase(newList); // <-- HERE
tempList.init();
tabIndex.value = 0;
Navigator.of(context).pop();
}
},
),
)
],
),
),
),
],
),
);
}
然后到函数的时候就出现空了!!
Future addListToDatabase(SubjectiveList list) async { <-- HERE
DocumentReference listDocument =
await userDocument.collection('lists').add({'name': list.name});
[...]
}
谢谢@edenar 现在我明白发生了什么。在 Flutter 中,“final newList = SubjectiveList(name: tempList.list.name, items: tempList.list.items);” 进行指针引用,而不是当前值的声明。因此,当它转到下一行并执行 tempList.init() 时,它会在获取函数中的参数之前清除列表。
因此,它可以将 await 放在该行中。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句