gpt4 book ai didi

asynchronous - 为 await block 中的对象字段分配新值进一步说明

转载 作者:IT王子 更新时间:2023-10-29 07:21:02 25 4
gpt4 key购买 nike

我卡在这上面了,我不知道为什么最后一次打印不执行对象值初始化。

await _client
.get(Uri.parse(_url), headers: {"location": "Mars"})
.then((result) => result.body)
.then(json.decode)
.then((json) => json.forEach((person) {
print(person); // this gets executed and printed over and over
Person newPerson;
print('hehe lolz'); // this gets executed too
newPerson.status = person['status'];
print('hello'); // this never gets executed...

Person 类只是一个包含所有 String 字段和一个构造函数的模型。

什么可能会阻止上次打印的执行?

最佳答案

那是因为 newPerson 为空,当您尝试调用 newPerson.status 时 - 出现 NullPointer 异常并且下面的所有代码都不会执行。只需:

Person newPerson = Person();

关于asynchronous - 为 await block 中的对象字段分配新值进一步说明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55634688/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com