【发布时间】:2021-12-26 13:45:25
【问题描述】:
当我使用 Firebase 存储上传图片时,它不会显示在屏幕上,直到我进行热刷新。所以问题是:当我从画廊或相机中挑选它时,如何自动显示它?谢谢。
代码:
late File file;
var imagePicker = ImagePicker();
var imgUrl;
var refStorage;
uploadPosterImage() async {
var pickedImage = await imagePicker.getImage(source: ImageSource.gallery);
if (pickedImage != null) {
setState(() {
file = File(pickedImage.path);
});
var imageName = basename(pickedImage.path);
var rand = Random().nextInt(10000000);
imageName = "$rand$imageName"; // random number + image name
refStorage = FirebaseStorage.instance.ref("items/$imageName");
await refStorage.putFile(file);
imgUrl = await refStorage.getDownloadURL();
}
}
------- 这就是正文中的代码:
Container(
margin: EdgeInsets.only(bottom: 10),
height: 230,
width: 160,
decoration: BoxDecoration(
border: Border.all(color: Colors.yellow),
borderRadius: BorderRadius.circular(20)),
child:
//imgUrl==null?
InkWell(
onTap: ()async{
await uploadPosterImage();
},
child: imgUrl == null? _icon : Image(image: NetworkImage(imgUrl),) )
),
【问题讨论】: