我通过下面给出的代码将图像从移动设备上传到Firebase存储,问题是我想向最终用户显示上传的图像,但URLs.json文件返回null,这是因为firebase实时数据库是空值。那么如何将图像存储在实时数据库中并获取URLs.json文件以在应用程序中调用它,或者如何获取URLs.json文件进行Firebase存储呢?
上传到Firebase存储-
FirebaseStorage _storage = FirebaseStorage.instance;
Future uploadPic(File file) async {
if (file != null) {
setState(() {
isLoading = true;
});
var rng = new Random();
String randomId = rng.nextInt(10000000).toString();
String fileName = "" +
randomId +
'${file.absolute.path.endsWith('.png') ? '.png' : '.jpg'}';
StorageReference reference = _storage.ref().child(fileName);
StorageUploadTask uploadTask = reference.putFile(file);
await (await uploadTask.onComplete).ref.getDownloadURL().then((url) {
setState(() {
_imageUrl = url.toString();
updateRTDatabaseWithURL(_imageUrl, randomId);
showInSnackBar('File Uploaded Successfully');
_image = null;
isLoading = false;
});
});
} else
showInSnackBar('Please, Choose a File');
}
List<dynamic> urlList;
updateRTDatabaseWithURL(imageURL, id) async {
var url = 'https://<Project_Name>.firebaseio.com/URLs.json';
await http.get(url).then((res) {
if (res.statusCode == 200) {
var resData = json.decode(res.body);
setState(() {
urlList = resData['URL'];
});
urlList.add(imageURL);
FirebaseDatabase()
.reference()
.child('URLs')
.update({'URL': urlList}).then((v) {
showInSnackBar('URL added to the Firebase List');
}).catchError((e) {
print(e.message);
});
}
});
}