如何将图像数据从FirebaseStorage保存到数据库并使用Flutter中的CircleAvatar检索它们

问题描述

您好,我想将我的Firebase存储中的图像数据插入数据库中,但是我在使用Floor用于数据库的过程中所有的想法都没有奏效。

这是我的代码,用于从Firebase获取图像将图像存储到数据库

var image = await ImagePicker.pickImage(source: ImageSource.gallery);
  imgString = Utility.base64String(image.readAsBytesSync()); //here where i get the image
  setState(() {
    _image = image;
    print('Image Path $_image');
  });
}
Future uploadPic(BuildContext context) async {
  String fileName = Path.basename(_image.path);
  StorageReference firebaseStorageRef = FirebaseStorage.instance.ref()
      .child(fileName);
  StorageUploadTask uploadTask = firebaseStorageRef.putFile(_image);
  StorageTaskSnapshot taskSnapshot = await uploadTask.onComplete;
 // setState(() {
    print("Profile Picture uploaded");
   //  Scaffold.of(context).showSnackBar(SnackBar(content: Text('Profile Picture Uploaded')));
    Fluttertoast.showToast(
        msg: 'New Data was Uploaded',toastLength: Toast.LENGTH_SHORT,timeInSecForIosWeb: 1,gravity: ToastGravity.BOTTOM);
 // });
}

这是我的代码,用于将图像数据保存到数据库

 insertData() async {
if(widget.firstnameController.text.isNotEmpty && widget.lastnameController.text.isNotEmpty &&
    widget.contactnumberController.text.isNotEmpty && widget.birthdayController.text.isNotEmpty) {
  var addContacts = ContactObject(
      firstname: widget.firstnameController.value.text,lastname: widget.lastnameController.value.text,birthday: widget.birthdayController.value.text,contactnumber: int.parse(widget.contactnumberController.value.text),profilepicture: Utility.imageFromBase64String(imgString).toString()); //Here is the image data
  await _contactsDao.insertContact(addContacts);
} else {
  print('ERROR');
}
Future.delayed(Duration(seconds: 2)).then((value) {
  Navigator.pushReplacement(context,MaterialPageRoute(
      builder: (BuildContext context) => Contactscreen()));
});

}

这是我在我的代码中对base64数据进行隐蔽的地方

    class Utility {

  static Image imageFromBase64String(String base64String) {
    return Image.memory(
      base64Decode(base64String),fit: BoxFit.fill,);
  }

  static Uint8List dataFromBase64String(String base64String) {
    return base64Decode(base64String);
  }

  static String base64String(Uint8List data) {
    return base64Encode(data);
  }
}

在此处我会在此处调用/显示图像数据以显示circleAvatar

                   CircleAvatar(
                        backgroundColor: Colors.white70,child: ClipOval(
                          child: new SizedBox(
                            // width: 180.0,// height: 180.0,child: Image.file(File('${listContact[index].profilepicture}'),),

H ere是异常错误

Cannot open file,path = 'Image(image: MemoryImage(Uint8List#1a942,scale: 1.0),frameBuilder: null,loadingBuilder: null,fit: fill,alignment: center,this.excludeFromSemantics: false,filterQuality: low)' (OS Error: No such file or directory,errno = 2)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)