Error: The await expression can only be used in an async function. Try marking the function body with either 'async' or 'async*'

1,338

To use await, you have to make your function async :

Future<Widget> getData(user) async {
  DocumentSnapshot snapShot = await Firestore.instance.collection('members')
                                                      .document(user.uid).get();
  //defining the user id
    List<String> hr = [
      "4xldjjaJKBOppV0ioZLaIekVNdr1",  //shreya [email protected]
      "20D18eFrsnRNmbtVnnvq8U0ZW1s2"   //pranav [email protected]
    ];
    List<String> doc = [
      "3DoGWLpamEVwXyT57hCyybkGIkc2",  //harsha [email protected]
      "YBHsJC5W6IYa1qfdIaXAwbvKKOG2",  //saampatii [email protected]
    ];
    List<String> gs = [
      "5QCDzv1YjQhQdLORJ7CthfATeHt1"   //digu [email protected]
    ];


    if(user == null){
      return Authenticate();
    }else if(snapShot == null || !snapShot.exists){return MemberData();}
    else{
      if(user.uid == hr[0]){return Hr();}
      else if(user.uid == hr[1]){return Hr();}
      else if(user.uid == doc[0]){return Doc();}
      else if(user.uid == doc[1]){return Doc();}
      else if(user.uid == gs[0]){return Gs();}
      return Home();
    }

}
Share:
1,338
Samyak Jain
Author by

Samyak Jain

Updated on December 23, 2022

Comments

  • Samyak Jain
    Samyak Jain over 1 year
    class Wrapper extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
    
        final user = Provider.of<User>(context);
        DocumentSnapshot snapShot = await Firestore.instance.collection('members').document(user.uid).get(); //re
    
        //defining the user id
        List<String> hr = [
          "4xldjjaJKBOppV0ioZLaIekVNdr1",  //shreya [email protected]
          "20D18eFrsnRNmbtVnnvq8U0ZW1s2"   //pranav [email protected]
        ];
        List<String> doc = [
          "3DoGWLpamEVwXyT57hCyybkGIkc2",  //harsha [email protected]
          "YBHsJC5W6IYa1qfdIaXAwbvKKOG2",  //saampatii [email protected]
        ];
        List<String> gs = [
          "5QCDzv1YjQhQdLORJ7CthfATeHt1"   //digu [email protected]
        ];
    
    
        if(user == null){
          return Authenticate();
        }else if(snapShot == null || !snapShot.exists){return MemberData();}
        else{
          if(user.uid == hr[0]){return Hr();}
          else if(user.uid == hr[1]){return Hr();}
          else if(user.uid == doc[0]){return Doc();}
          else if(user.uid == doc[1]){return Doc();}
          else if(user.uid == gs[0]){return Gs();}
          else{return Home();}
        }
      }
    }
    

    What can I do to remove the error?

    There I tried checking if the document with that uid is stored or not in my firestore. Is there any other way without getting an error?

  • Samyak Jain
    Samyak Jain over 3 years
    Doing this another error occurred: This function has a return type of 'Widget', but doesn't end with a return statement. Try adding a return statement, or changing the return type to 'void'.
  • fatalcoder524
    fatalcoder524 over 3 years
    change Future to Future<Widget>. that shud fix. and return the output of that function call.
  • Samyak Jain
    Samyak Jain over 3 years
    This is not working, can you suggest some other solution.
  • Samyak Jain
    Samyak Jain over 3 years
    Error: This function has a return type of 'Widget', but doesn't end with a return statement. Try adding a return statement, or changing the return type to 'void'. Error: The value of the local variable 'user' isn't used. Try removing the variable, or using it. Error: The declaration 'getData' isn't referenced. Try removing the declaration of 'getData'.
  • fatalcoder524
    fatalcoder524 over 3 years
    Will modify the code.. changes:- in else part of code remove else and just return. That shud fix it.