在处理上一行结果之前是否正在执行代码?

时间:2019-09-06 04:16:37

标签: flutter

我在共享首选项中有一个列表,我需要将其动态绑定到Tab View。当我尝试绑定时,它会遇到错误

引发了以下断言

 The following assertion wa thrown building HomeScreen(dirty, state: _HomeScreenState#c16f7):
 'package:flutter/src/material/tab_controller.dart': Failed assertion: line 316 pos 15: 'initialIndex
>= 0 && initialIndex < length': is not true.

@override
Widget build(BuildContext context) {

 getPrefList();

 List<Widget> tabList = new List();
    for (int i = 0; i < prefList.length; i++) {
      Tab tab = Tab(text: prefList[i]);
      tabList.add(tab);
    }

return MaterialApp(
      home: DefaultTabController(
        length: prefList.length,
        child: Scaffold(
          appBar: AppBar(
            bottom: TabBar(
              tabs: tabList,
            ),
            title: Text('Tab Layout'),
          ),
          body: TabBarView(
            children: [
              Icon(Icons.directions_car),
              Icon(Icons.directions_transit),
              Icon(Icons.directions_bike),
            ],
          ),
        ),
      ),
    );    
}

Future getPrefList() async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  prefList.addAll(prefs.getStringList("prefList"));
}

在构建标签布局之前,我需要共享首选项的结果!

1 个答案:

答案 0 :(得分:0)

  

这有效

@override
Widget build(BuildContext context) {
 var _asyncLoader = new AsyncLoader(
   key: asyncLoaderState,
   initState: () async => getPrefList(),
   renderLoad: () => Center(child: new CircularProgressIndicator()),
   renderError: ([error]) => getNoConnectionWidget(),
   renderSuccess: ({data}) => setTabList(),
 );

 return Scaffold(
   // Create a Listview and load the data when available
   body: new Scrollbar(
     child: Container(child: _asyncLoader),
   ),
 );
}