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

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 'till a Stream runs.

My initState method is:

 @override
  void initState () {
    super.initState();
    _googleSignIn.onCurrentUserChanged.listen((GoogleSignInAccount account)     {
      setState(() {
        _currentUser = account;
      });
    });
    _googleSignIn.signInSilently();
  }

I will appreciate any feedback.


You can create an async method and call it inside your initState

       @override
        void initState () {
          super.initState();
          WidgetsBinding.instance.addPostFrameCallback((_){
            _asyncMethod();
          });

        }

        _asyncMethod() async {
         _googleSignIn.onCurrentUserChanged.listen((GoogleSignInAccount account)     {
            setState(() {
              _currentUser = account;
            });
          });
          _googleSignIn.signInSilently();
        }

As of now using .then notation seems to work:

  // ...
  @override
  initState() {
    super.initState();
    myAsyncFunction
    // as suggested in the comment
    // .whenComplete() {
    // or
      .then((result) {
    print("result: $result");
    setState(() {});
    });
  }
  //...

Method 1 : You can use StreamBuilder to do this. This will run the builder method whenever the data in stream changes.

Below is a code snippet from one of my sample projects:

StreamBuilder<List<Content>> _getContentsList(BuildContext context) {
    final BlocProvider blocProvider = BlocProvider.of(context);
    int page = 1;
    return StreamBuilder<List<Content>>(
        stream: blocProvider.contentBloc.contents,
        initialData: [],
        builder: (context, snapshot) {
          if (snapshot.data.isNotEmpty) {
            return ListView.builder(itemBuilder: (context, index) {
              if (index < snapshot.data.length) {
                return ContentBox(content: snapshot.data.elementAt(index));
              } else if (index / 5 == page) {
                page++;
                blocProvider.contentBloc.index.add(index);
              }
            });
          } else {
            return Center(
              child: CircularProgressIndicator(),
            );
          }
        });
  }

In the above code StreamBuilder listens for any change in contents, initially its an empty array and shows the CircularProgressIndicator. Once I make API call the data fetched is added to contents array, which will run the builder method.

When the user scrolls down, more content is fetched and added to contents array which will again run builder method.

In your case only initial loading will be required. But this provides you an option to display something else on the screen till the data is fetched.

Hope this is helpful.

EDIT:

In your case I am guessing it will look something like shown below:

StreamBuilder<List<Content>>(
        stream: account, // stream data to listen for change
        builder: (context, snapshot) {
            if(account != null) {
                return _googleSignIn.signInSilently();
            } else {
                // show loader or animation
            }
        });

Method 2: Another method would be to create an async method and call it from you initState() method like shown below:

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

  void asyncMethod() async {
    await asyncCall1();
    await asyncCall2();
    // ....
  }