【发布时间】:2019-09-22 23:53:50
【问题描述】:
我编写了一个程序将照片保存在 Firebase 存储中,然后获取一个 url 并将其写入 Firebase 数据库。
File sampleImage;
String changePhoto;
Future getImage() async {
var tempImage = await ImagePicker.pickImage(source: ImageSource.gallery);
setState(() {
sampleImage = tempImage;
});
}
Future getUrl(name) async
{
final ref = FirebaseStorage.instance.ref().child(name);
changePhoto = await ref.getDownloadURL();
print(changePhoto);
return changePhoto;
}
Widget submitButton() {
final name = productName.text;
return RaisedButton(
textColor: Colors.white,
color: Color(0xffd50000),
child: Text("Add"),
shape: RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(30.0)),
onPressed: () {
final StorageReference firebaseStorageRef =
FirebaseStorage.instance.ref().child(name);
final StorageUploadTask task =
firebaseStorageRef.putFile(sampleImage);
getUrl(name);
_bloc.submit(changePhoto);
print(5);
Navigator.of(context).pop();
});
}
但我有问题
changePhoto = 等待 ref.getDownloadURL();
因为 ref.getDownloadURL() 返回一个动态变量。我试过用
Future loadPhoto(name)
{
final StorageReference firebaseStorageRef =
FirebaseStorage.instance.ref().child(name);
final StorageUploadTask task =
firebaseStorageRef.putFile(sampleImage);
}
onPressed: () {
loadPhoto(name).then((snapshot){
final ref = FirebaseStorage.instance.ref().child(name);
changePhoto = snapshot.ref.getDownloadURL();
print(changePhoto);
_bloc.submit(changePhoto);
print(5);
Navigator.of(context).pop();
});
但它不起作用。我的错误是什么或如何获取快照? 非常感谢任何帮助。
【问题讨论】:
标签: android dart flutter firebase-storage