【问题标题】:Error: The await expression can only be used in an async function. Try marking the function body with either 'async' or 'async*'错误:await 表达式只能在异步函数中使用。尝试用“async”或“async*”标记函数体
【发布时间】:2020-11-30 11:38:00
【问题描述】:
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 shreya.21810101@viit.ac.in
      "20D18eFrsnRNmbtVnnvq8U0ZW1s2"   //pranav pranav.21810339@viit.ac.in
    ];
    List<String> doc = [
      "3DoGWLpamEVwXyT57hCyybkGIkc2",  //harsha harsha.21810512@viit.ac.in
      "YBHsJC5W6IYa1qfdIaXAwbvKKOG2",  //saampatii saampatii.21810826@viit.ac.in
    ];
    List<String> gs = [
      "5QCDzv1YjQhQdLORJ7CthfATeHt1"   //digu digvijay.21810245@viit.ac.in
    ];


    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();}
    }
  }
}

我可以做些什么来消除错误?

在那里,我尝试检查具有该 uid 的文档是否存储在我的 Firestore 中。有没有其他方法不报错?

【问题讨论】:

    标签: firebase flutter google-cloud-firestore async-await future


    【解决方案1】:

    要使用 await,你必须让你的函数异步:

    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 shreya.21810101@viit.ac.in
          "20D18eFrsnRNmbtVnnvq8U0ZW1s2"   //pranav pranav.21810339@viit.ac.in
        ];
        List<String> doc = [
          "3DoGWLpamEVwXyT57hCyybkGIkc2",  //harsha harsha.21810512@viit.ac.in
          "YBHsJC5W6IYa1qfdIaXAwbvKKOG2",  //saampatii saampatii.21810826@viit.ac.in
        ];
        List<String> gs = [
          "5QCDzv1YjQhQdLORJ7CthfATeHt1"   //digu digvijay.21810245@viit.ac.in
        ];
    
    
        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();
        }
    
    }
    

    【讨论】:

    • 执行此操作发生了另一个错误:此函数的返回类型为“Widget”,但不以 return 语句结束。尝试添加 return 语句,或将返回类型更改为 'void'。
    • Future 更改为Future&lt;Widget&gt;。那是修复。并返回该函数调用的输出。
    • 这行不通,您能否提出其他解决方案。
    • 错误:此函数的返回类型为“Widget”,但不以 return 语句结束。尝试添加 return 语句,或将返回类型更改为“void”。错误:未使用局部变量“用户”的值。尝试删除变量或使用它。错误:未引用声明“getData”。尝试删除“getData”的声明。
    • 将修改代码.. 更改:- 在代码的 else 部分删除 else 并返回。那就修好了。
    猜你喜欢
    • 1970-01-01
    • 2020-05-26
    • 2021-05-02
    • 1970-01-01
    • 2019-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-02
    相关资源
    最近更新 更多