1

I make initPlatformState .. an async function to detect mock location and after the process of initPlatformState function is finished I need to do something...but the problem is... because initPlatformState is working asynchronously and I am not putting initPlatformState inside initState... initPlatformState will work before it is not done yet... is there a way to wait initPlatformState has done doing the process and then I will do something else? here is my code

bool canMockLocation=false;
initPlatformState() async {
    if (!mounted) return;
    try {
      canMockLocation = await TrustFall.canMockLocation;
    } catch (error) {
      print(error);
    }

    setState(() {
      canMockLocation = canMockLocation;
    });
  }

_getData() async {
       setState(() {
        load = true;
      });
      initPlatformState();
      setState(() {
        load = false;
      });
       if(canMockLocation){
        show alert;
       }else{
        print("check");
       }

and here is for my widget

             Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      loadLocation
                          ? Text("Loading...") 
                          : Text("Success")
             .....

1 Answer 1

2

add await where you are calling this method. await will hold position till function process is not complete.

await initPlatformState();
Sign up to request clarification or add additional context in comments.

4 Comments

I wanna ask.. you said that await will hold until the process is not complete... what do you mean by not complete?
as your initPlatformState method is async so if you add await where you are calling(in _getData method) then until that function's each line is not executed next line of _getData will not exicuted.
so using await initPlatformState()...getData will be executed until the process inside initPlatformState() is done... is that right if I say llike that?
yes, to be more clear. even this line will be not executed load = false;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.