Dart/Flutter:当作为参数传递时,List 中的元素字符串变为空(为啥??)
Posted
技术标签:
【中文标题】Dart/Flutter:当作为参数传递时,List 中的元素字符串变为空(为啥??)【英文标题】:Dart/Flutter: Strings of element inside a List becomes empty when passing as an argument (Why??)Dart/Flutter:当作为参数传递时,List 中的元素字符串变为空(为什么??) 【发布时间】:2020-09-26 23:35:12 【问题描述】: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);
[...]
【问题讨论】:
尝试使用 await >> await DatabaseService(uid: userData.uid) .addListToDatabase(newList); 它与异步等待一起工作!太感谢了!现在我明白发生了什么。在 Flutter 中,行“final newList = SubjectiveList(name: tempList.list.name, items: tempList.list.items);”进行指针引用,而不是当前值的声明。因此,当它转到下一行并执行 tempList.init() 时,它会在获取函数中的参数之前清除列表。我真正不明白的是它以前在工作......我在过去的两天里只是在改变设计,不知何故它在那里改变了。可能我已经更改了线路顺序,但我不记得了。 同样的事情发生在我身上。当调用list.clear()
时,您传递的列表也将被清除。谢谢你的提示@HyungTaeCarapetoFigur
你也可以在这里查看答案***.com/questions/58389591/…
【参考方案1】:
感谢@edenar 现在我明白发生了什么。在 Flutter 中,行“final newList = SubjectiveList(name: tempList.list.name, items: tempList.list.items);”进行指针引用,而不是当前值的声明。因此,当它转到下一行并执行 tempList.init() 时,它会在获取函数中的参数之前清除列表。
所以它在该行中放置了等待。
【讨论】:
以上是关于Dart/Flutter:当作为参数传递时,List 中的元素字符串变为空(为啥??)的主要内容,如果未能解决你的问题,请参考以下文章
Dart/Flutter WebSocket 服务器/客户端在同一侧无法正常工作
如何在 Dart / Flutter 中使用另一个文件的函数?
在Python中,当作为类方法定义中的默认值传递时,赋值运算符是否会访问类或实例变量?