它总是返回null setState()

时间:2020-03-05 06:33:15

标签: firebase flutter google-cloud-firestore google-cloud-storage firebase-storage

我正在将图像上传到Firebase存储中,然后下载URL以稍后检索图像。 我添加了屏幕截图和更多代码。 在print(url)中工作正常,当将值URL传递到storenewuser函数时,它存储null

This is the upload function:
Future uploadPic() async{
    String filename = phoneNumber;
    StorageReference ref = FirebaseStorage.instance.ref().child("User Profile Image").child(filename);
    StorageUploadTask uploadTask = ref.putFile(_Image);
    var downUrl = await (await uploadTask.onComplete).ref.getDownloadURL();
    var url = downUrl.toString();
    print(url);
    if (!mounted) return;
    setState(() {
      ImageUrl=url;
    });
  }
SignUp and storing usersdata:
void Signup() async{
    if(_key.currentState.validate()){
      FirebaseAuth.instance.createUserWithEmailAndPassword(email: Email, password: Password).then((signedInUser){
        uploadPic();
        UserManagement().storeNewUser(Name,Email,phoneNumber,ImageUrl,RollNumber,Course,Semester,signedInUser.user, context);
      }).catchError((e){
        final snackBar = SnackBar(
          content: Text(e.message),
        );
        Scaffold.of(context).showSnackBar(snackBar);
        print(e);
      });
    }
    else{
      print("NULL");
    }
  }

storeNewUser(Name,Email/*,password*/,phonenumber,ImageUrl,rollnumber,course,sem,user,context){
    Firestore.instance.collection('/users').document(user.uid).setData({
      'Name' : Name,
      'Email' : Email,
      'Image Url' : ImageUrl,
      'Phone Number' : phonenumber,
      'Roll Number' : rollnumber,
      'College' : 'Siliguri Institute of Technology',
      'Course' : course,
      'Semester' : sem
    }).then((value){
        Navigator.of(context).pop();
        Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => SignUpCompletePage()));
    }).catchError((e){
      final snackBar = SnackBar(
        content: Text(e.message),
      );
      Scaffold.of(context).showSnackBar(snackBar);
    });
  }

数据库:

enter image description here

存储空间:

enter image description here

1 个答案:

答案 0 :(得分:3)

只是改变

uploadPic();

await uploadPic();

:)