flatter firebase setData()函数在异步函数完成之前为字段指定空值

flatter firebase setData()函数在异步函数完成之前为字段指定空值,firebase,flutter,asynchronous,dart,google-cloud-firestore,Firebase,Flutter,Asynchronous,Dart,Google Cloud Firestore,我正在尝试将用户的头像上载到firestore存储,并返回图像url,以根据用户实例上的用户头像url属性保存它。我能够成功地将图像上载到firestore。该问题与url的接收返回有关。异步函数会延迟,因此前面的用户实例化函数会继续使用avatarUrl属性的空值 这是功能启动 if(_formKey.currentState.validate()){ setState(() {

我正在尝试将用户的头像上载到firestore存储,并返回图像url,以根据用户实例上的用户头像url属性保存它。我能够成功地将图像上载到firestore。该问题与url的接收返回有关。异步函数会延迟,因此前面的用户实例化函数会继续使用avatarUrl属性的空值

这是功能启动

                    if(_formKey.currentState.validate()){
                      setState(() {
                        loading=true;
                      });
                      await uploadFile();
                      print('Print before async function complete...');
                      print(_avatarUrl);
                      dynamic result=  await SellerDatabaseService(uid:user.uid).sellerProfileSetup(
                        shopName: shopName,
                        phone: phone,
                        bio: bio,
                        location: location,
                        avatarUrl: _avatarUrl,
                        rating: 0,
                        sales: 0,
                        credit: 0,
                        posts: 0,
                        joinedDate:DateTime.now(),
                        //todo remove null from profile image
                      );
                      if(result==null){
                        setState(() {
                          loading=false;
                          error='Please supply correct details';
                        });
                      }
                    }
                  },
这里是上传功能

                    if(_formKey.currentState.validate()){
                      setState(() {
                        loading=true;
                      });
                      await uploadFile();
                      print('Print before async function complete...');
                      print(_avatarUrl);
                      dynamic result=  await SellerDatabaseService(uid:user.uid).sellerProfileSetup(
                        shopName: shopName,
                        phone: phone,
                        bio: bio,
                        location: location,
                        avatarUrl: _avatarUrl,
                        rating: 0,
                        sales: 0,
                        credit: 0,
                        posts: 0,
                        joinedDate:DateTime.now(),
                        //todo remove null from profile image
                      );
                      if(result==null){
                        setState(() {
                          loading=false;
                          error='Please supply correct details';
                        });
                      }
                    }
                  },
这是输出

如图所示,url稍后会在
SellerDatabaseService(uid:user.uid)之后返回。sellerProfileSetup()
函数已运行。所以我在avatarUrl属性上得到一个空值。我如何防止这种情况? 提前感谢。

更改此选项:

    storageReference.getDownloadURL().then((fileURL) {
      setState(() {
        _avatarUrl = fileURL;
        print('Print after async function completes...');
        print(_avatarUrl);

      });
    });
为此:

    var fileURL = await storageReference.getDownloadURL();
      setState(() {
        _avatarUrl = fileURL;
        print('Print after async function completes...');
        print(_avatarUrl);

      });
    });
使用
wait
代替
then()
。当前,您正在输入方法
uploadFile()
,但由于
getDownloadURL()
是异步的,因此即使在获取url之前也会执行
print()
方法

    var fileURL = await storageReference.getDownloadURL();
      setState(() {
        _avatarUrl = fileURL;
        print('Print after async function completes...');
        print(_avatarUrl);

      });
    });