无法从Firebase返回查询的数据(颤振/飞镖)

无法从Firebase返回查询的数据(颤振/飞镖),firebase,flutter,dart,google-cloud-firestore,Firebase,Flutter,Dart,Google Cloud Firestore,上下文:我试图从Firebase查询并返回一个字符串(imgUrl)。我总是能够在查询中打印字符串,但是返回的值总是空的。我想知道我的查询是否有误,也不确定最佳实践是什么 数据库大纲: 查询功能: 这是DatabaseService()类下的代码,该类包含所有数据库查询和更新函数 String getImageUrl(String\u uid){ 字符串\u imgUrl; Firestore.instance .document('users/$\u-uid') .get() 。然后((val

上下文:我试图从Firebase查询并返回一个字符串(imgUrl)。我总是能够在查询中打印字符串,但是返回的值总是空的。我想知道我的查询是否有误,也不确定最佳实践是什么

数据库大纲: 查询功能: 这是DatabaseService()类下的代码,该类包含所有数据库查询和更新函数

String getImageUrl(String\u uid){
字符串\u imgUrl;
Firestore.instance
.document('users/$\u-uid')
.get()
。然后((value)=>imgUrl=value['imgUrl']);
返回图;
}
Main: 在setImage()下调用getImageUrl()。setImage下的toast总是返回null,其下的代码也是如此

String\u uid;
//将变量“\u uid”设置为当前用户的uid
//在initstate中调用
Future\u getUid()异步{
FirebaseUser=等待FirebaseAuth.instance.currentUser();
_uid=user.uid;
}
//设置配置文件照片。如果网上没有现有的个人资料照片,
//抓取设备上的图像。如果在线或设备上没有图像,
//显示默认图像
void setImage(字符串url){
//获取存储在数据库中的url
字符串_tempUrl=DatabaseService().getImageUrl(_uid);//总是以null结束
flattertoast.showtoos(消息:“\u tempUrl:$\u tempUrl”);
//函数的其余部分
}
@凌驾
void initState(){
super.initState();
_getUid();
}

请让我知道如何解决这个问题,因为它让我发疯。提前感谢。

将方法更改为以下内容:

 Future<String> getImageUrl(String _uid) async {
    String _imgUrl;

    DocumentSnapshot value =
        await Firestore.instance.document('users/$_uid').get();
    _imgUrl = value['imgUrl'];
    return _imgUrl;
  }
  void setImage(String url) async{
     // Get the url that's stored in the db
     String _tempUrl = await DatabaseService().getImageUrl(_uid); // always ends up being null
     Fluttertoast.showToast(msg: "_tempUrl: $_tempUrl");

     // Rest of the function
}

将方法更改为以下内容:

 Future<String> getImageUrl(String _uid) async {
    String _imgUrl;

    DocumentSnapshot value =
        await Firestore.instance.document('users/$_uid').get();
    _imgUrl = value['imgUrl'];
    return _imgUrl;
  }
  void setImage(String url) async{
     // Get the url that's stored in the db
     String _tempUrl = await DatabaseService().getImageUrl(_uid); // always ends up being null
     Fluttertoast.showToast(msg: "_tempUrl: $_tempUrl");

     // Rest of the function
}