网络请求之前,接收数据的对象,肯定是为初始化的,所以有异常,如何解决
来源:12-12 网格卡片功能实现&布局技巧-2【布局进阶】
Junble
2022-09-24
初始化变量
List<CommonModel> localNavList = [];
late GridNavModel gridNavModel;
赋值
loadData() async {
try {
HomeModel model = await HomeDao.fetch();
setState(() {
localNavList = model.localNavList;
gridNavModel = model.gridNav;
});
} catch (e) {
print(e.toString());
}
}
使用
GridNav(gridNavModel: gridNavModel),
Container(
height: 800,
child: ListTile(
title: Text(''),
),
)
GridNav(gridNavModel: gridNavModel), 这里报异常
提示
LateInitializationError: Field ‘gridNavModel’ has not been initialized.
忽略这个异常之后,页面还是能正常出来的
写回答
1回答
-
加个条件判断吧,当gridNavModel不为空的时候在初始化GridNav
if(gridNavModel != null) GridNav(gridNavModel: gridNavModel),
012022-09-26
相似问题