1

how to pass context in async gap?

Future<void> somethingMustWait(BuildContext context) async{
    String? myStringFromAPi;
    
    bool? imustWait;
    
    //api
    await Future<String>.delayed(const Duration(seconds: 2), (){
      return 'hello World';
    }).then((value){
      imustWait = true;
      removeOldString();
      myStringFromAPi = value;
    }, onError: (e){
      imustWait = false;
    }).timeout(const Duration(seconds: 5));
    
    //then if result never return, i will wait forever <3
    await Future.doWhile(() => imustWait == null);
    
    // Call context here to show dialog:
    showDialog(context: context, builder: (context)=> AlertDialog(content: Text("yo finished : $myStringFromAPi"),));
    
  }

let say, something happen in this async gap, and context no longer valid. maybe because removeOldString() function Rebuild ui, or the ui stream to data changes. then my showDialog will get invalid context right? how i pass current valid context ?

Sayyid J
  • 1,215
  • 1
  • 4
  • 18

0 Answers0