作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 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/
我是一名优秀的程序员,十分优秀!