【问题标题】:Upload and save image into firebase Storage flutter dart将图像上传并保存到firebase Storage中
【发布时间】:2021-08-22 18:48:08
【问题描述】:

我正在尝试上传图片并将用户信息注册到 firebase 存储和 firestore 问题是由于某种原因它不接受此代码它说我需要在 _registerUser() 之后放置 ; 这是我用来保存和上传信息的代码:

 Future<void> uploadAndSaveImage() async {
if (_imageFile != null) {
  _passwordTextEditingController.text ==
          _cPasswordTextEditingController.text
      ? _emailTextEditingController.text.isNotEmpty &&
              _passwordTextEditingController.text.isNotEmpty &&
              _cPasswordTextEditingController.text.isNotEmpty &&
              _nameTextEditingController.text.isNotEmpty
          ?uploadToStorage() , _registerUser()
          : displayDialog("Please fill up the registration complete form..")
      : displayDialog("Password do not match.");
} else {
  showDialog(
      context: context,
      builder: ((c) {
        return ErrorAlertDialog(
          message: "Please select an image.",
        );
      }));
}

}

注册用户功能:

FirebaseAuth _auth = FirebaseAuth.instance;

void _registerUser() 异步 { FirebaseUser firebaseUser;

await _auth
    .createUserWithEmailAndPassword(
  email: _emailTextEditingController.text.trim(),
  password: _passwordTextEditingController.text.trim(),
)
    .then((auth) {
  firebaseUser = auth.user;
}).catchError((error) {
  Navigator.pop(context);
  showDialog(
      context: context,
      builder: (c) {
        return ErrorAlertDialog(
          message: error.message.toString(),
        );
      });
});
if (firebaseUser != null) {
  saveUserInfoToFireStore(firebaseUser).then((value) {
    Navigator.pop(context);
    Route route = MaterialPageRoute(builder: (c) => StoreHome());
    Navigator.pushReplacement(context, route);
  });
}

}

Ps:我还是新手,这是我的第三个 Flutter 应用

【问题讨论】:

    标签: firebase flutter dart firebase-storage


    【解决方案1】:

    我已使用此代码将用户图像从本地存储上传到 Firebase,并以 uid 作为参考名称。

    final _firebaseAuth = FirebaseAuth.instance;
      FirebaseStorage firebaseStorage = FirebaseStorage.instance;
      FirebaseFirestore firebaseFirestore = FirebaseFirestore.instance;
      final imgPicker = ImagePicker();
      File image;
      String photoUrl = "";
      bool _uploading = false;
    
      uploadPic() async {
        try {
          setState(() {
            _uploading = true;
          });
    
          // picking Image from local storage
          final file = await imgPicker.getImage(
            source: ImageSource.gallery,
          );
    
          if (file != null) {
            image = File(file.path);
          } else {  
            setState(() {
              _uploading = false;
            });
          }
    
          // creating ref at Firebase Storage with userID
          Reference ref =
              firebaseStorage.ref(_firebaseAuth.currentUser.uid).child("dp");
    
          ref.putFile(image).whenComplete(() {
            print("Pic Uploaded Successfully!");
            setState(() {
              _uploading = false;
            });
            // refreshing the UI when photo updated
            getUploadedPic();
          });
        } catch (e) {
          print(e);
        }
      }
    

    上传后,您可能会得到如下图片网址:

      getUploadedPic() async {
        // getting dp URL link
        photoUrl = await firebaseStorage
            .ref("${_firebaseAuth.currentUser.uid}/dp")
            .getDownloadURL()
            .whenComplete(() => print("URL UPLOADED AT: $photoUrl"));
    
      }
    

    【讨论】:

      猜你喜欢
      • 2017-07-28
      • 2021-02-06
      • 2019-07-22
      • 2021-03-14
      • 2020-03-18
      • 1970-01-01
      • 2017-09-25
      相关资源
      最近更新 更多