我正在从 Flutter 中的 url 获取数据。我有一个名为表的后期初始化地图。我在 initState() 函数中获取数据。当我第一次启动应用程序时,我在红屏上收到 LateInizialization 错误。我想检测延迟初始化是否已初始化,如果未初始化则显示微调器。这是我的代码。
class TablesTab extends StatefulWidget {
@override
_TablesTabState createState() => _TablesTabState();
}
class _TablesTabState extends State<TablesTab> {
late List<dynamic> tables;
var refreshKey = GlobalKey<RefreshIndicatorState>();
GetUriData tablesInstance = new GetUriData(url: '/api/table/getAllTables');
void getTables() async {
tables = await tablesInstance.getData();
}
@override
void initState() {
super.initState();
getTables();
}
Future<void> refreshList() async {
refreshKey.currentState?.show(atTop: false);
// await Future.delayed(Duration(seconds: 2));
var updatedTables = await tablesInstance.getData();
setState(() {
tables = updatedTables;
});
//network call and setState so that view will render the new values
print(tables);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: RefreshIndicator(
child: GridView.count(
crossAxisCount: 4,
children: List.generate(this.tables.length, (index) {
return Center(
child: GestureDetector(
onTap: () {
Navigator.pushNamed(context, '/tableReview', arguments: {'table': this.tables[index]});
},
child: Container(
width: 80,
height: 80,
decoration: BoxDecoration(
border: Border.all(color: Theme.of(context).primaryColor, width: 2.0),
borderRadius: BorderRadius.all(Radius.circular(70))
),
padding: const EdgeInsets.all(14.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
'${this.tables[index]['tableNum']}',
style: TextStyle(
color: Theme.of(context).primaryColor,
fontSize: 23
),
),
],
)
),
),
);
}),
),
onRefresh: refreshList
)
);
}