gpt4 book ai didi

flutter 错误 : The following NoSuchMethodError was thrown building

转载 作者:行者123 更新时间:2023-12-03 03:21:18 25 4
gpt4 key购买 nike

我是 flutter 的新手,每次运行应用程序时都会出错:

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following NoSuchMethodError was thrown building DataList(dirty, dependencies:
[_InheritedProviderScope<List<Store>>], state: _DataListState#67e37):
The getter 'length' was called on null.
Receiver: null
Tried calling: length
The relevant error-causing widget was:
DataList

这是我的数据列表文件:

class DataList extends StatefulWidget {
@override
_DataListState createState() => _DataListState();
}

class _DataListState extends State<DataList> {
@override
Widget build(BuildContext context) {
final stores = Provider.of<List<Store>>(context);

// stores.forEach((d) {
// print(d.name);
// });

return ListView.builder(
itemCount: stores.length,
itemBuilder: (context, index) {
return StoreTile(store: stores[index]);
},
);
}
}

这发生在我登录并显示商店列表之后。我打印了 stores.length 并返回了 6,但无论出于何种原因,应用程序发现了一个 null 并抛出错误。

请注意,应用程序的屏幕显示正确,但在调试控制台中出现此错误。

感谢任何帮助乔

最佳答案

您的提供商似乎需要一点时间来获取商店列表,因此 stores 暂时为空。您可以在 stores 为 null 时显示进度指示器,然后显示数据

class _DataListState extends State<DataList> {
@override
Widget build(BuildContext context) {
final stores = Provider.of<List<Store>>(context);

// stores.forEach((d) {
// print(d.name);
// });

return stores == null
? Center(child: CircularProgressIndicator())
: ListView.builder(
itemCount: stores.length,
itemBuilder: (context, index) {
return StoreTile(store: stores[index]);
},
);
}
}

关于 flutter 错误 : The following NoSuchMethodError was thrown building,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62932060/

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