Is there a way to load async data on InitState method?

前端 未结 9 1519
你的背包
你的背包 2020-12-05 16:39

I\'m a looking for a way to load async data on InitState method, I need some data before build method runs. I\'m using a GoogleAuth code, and I need to execute build method

9条回答
  •  無奈伤痛
    2020-12-05 17:30

    Sample code:

     @override
      void initState() {
        super.initState();
    
        asyncOperation().then((val) {
          setState(() {});
          print("success");
        }).catchError((error, stackTrace) {
          print("outer: $error");
        });
    
    //or
    
        asyncOperation().whenComplete(() {
          setState(() {});
          print("success");
        }).catchError((error, stackTrace) {
          print("outer: $error");
        });
      }
    
      Future asyncOperation() async {
        await ... ;
      }
    

提交回复
热议问题