首页 > 解决方案 > 使用 TabBarView 和 DefaultTabController 时的 initState()

问题描述

我在 initState 方法中从 firestore 加载数据。如果屏幕不是 a 的一部分,TabBarView()它会按预期工作,而TabBarView我必须热重新加载才能加载数据。

//我是如何创建 mt 选项卡的

return DefaultTabController(
  length: 4,
      child: Scaffold(
    appBar: AppBar(centerTitle: true,        
      title: Text("tabs", style: TextStyle(fontWeight: FontWeight.bold, color: Colors.white)),
      bottom: TabBar(

        indicatorColor: Colors.white,
        tabs: <Widget>[
          Tab(
            icon: Image.asset(
              "assets/icons/tools.png",
              height: 40,
            ),
            child: Text("tab0",style: TextStyle(fontSize: 10),),
          ),
          Tab(
            icon: Image.asset(
              "assets/icons/il.png",
              height: 40,
            ),
            child: Text("tab1",style: TextStyle(fontSize: 10)),
          ),
          Tab(
            icon: Icon(Icons.lock),
            child: Text("tab2"),
          ),
          Tab(
            icon: Icon(Icons.lock),
            child: Text("tab3"),
          ),
        ],
      ),
    ),
    body: TabBarView(children: <Widget>[
      Tab0(),
      Tab2(),
      Tab2(),
     Tab3()
    ],),
  ),
);

以下是我在 Tab3 中从 cloudfirestore 加载数据的方式:

getIssues() async {
  final FirebaseUser fbUser = await FirebaseAuth.instance.currentUser();
  final String user = fbUser.uid;
  return await Firestore.instance
    .collection('users')
    .document(user)
    .collection('mydata')
    .getDocuments();
}

我的initState

QuerySnapshot querySnapshot;

@override
void initState() {
  super.initState();
  getIssues().then((results) {
    querySnapshot = results;
  });
}

标签: firebasefluttergoogle-cloud-firestorefirebase-authentication

解决方案


也许你试试这个:

@override
void initState() {
  super.initState();
  getIssues().then((results) {
    setState(() {
      querySnapshot = results;
    });
  });
}

推荐阅读