TabBarView中的页面无法在initState中运行功能

时间:2019-12-17 08:30:42

标签: flutter dart google-cloud-firestore

我在tabbarView中有页面,每次查看该页面时都会运行initstate。但是,当我加载屏幕时,将不会显示任何数据。只有当我重新加载页面时,它才会出现。

这是页面本身代码的一部分:


class TabViewReusablePage extends StatefulWidget {
  final String rotaNumber;

  TabViewReusablePage({this.rotaNumber});

  @override
  _TabViewReusablePageState createState() => _TabViewReusablePageState();
}

class _TabViewReusablePageState extends State<TabViewReusablePage> {

List<ReusableListTile> paramedic = [];

void getInfo() async {
    final QuerySnapshot getdataPrm = await Firestore.instance
        .collection('Info')
        .document(widget.rotaNumber)
        .collection('Personnel')
        .getDocuments();



    getdataPrm.documents.forEach((f) {
      final title= f.data['title'];
      final code= f.data['code'];
      paramedic.add(ReusableListTile(
        title: title,
        code: code,
      ));
    });  
  }

@override
  void initState() {
    getInfo();
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

1 个答案:

答案 0 :(得分:1)

尝试一下:

class TabViewReusablePage extends StatefulWidget {
  final String rotaNumber;

  TabViewReusablePage({this.rotaNumber});

  @override
  _TabViewReusablePageState createState() => _TabViewReusablePageState();
}

class _TabViewReusablePageState extends State<TabViewReusablePage> {

List<ReusableListTile> paramedic = [];

void getInfo() async {
    final QuerySnapshot getdataPrm = await Firestore.instance
        .collection('Info')
        .document(widget.rotaNumber)
        .collection('Personnel')
        .getDocuments();



    getdataPrm.documents.forEach((f) {
      final title= f.data['title'];
      final code= f.data['code'];

      //Update paramedic state

       setState((){
         paramedic.add(ReusableListTile(
           title: title,
           code: code,
         ));   
       });

    });  
  }

@override
  void initState() {
    getInfo();
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }