【发布时间】:2021-07-21 14:22:44
【问题描述】:
我正在 Dart 中使用 Flutter 创建个人资料页面。用户信息存储在 Firebase Firestore 中,图像存储在 Firebase Storage 中。 Firestore 包含配置文件图像在 Firebase 存储中的路径 (profileImages/{userId}/profile.jpg)。正如您在下面看到我的代码,可下载的图片 url 是在 ProfileImage 类中获得的。
我想在初始化用户类时getDownloadUrl。不在 ProfileImage 类中。
class User {
final profilePathInFirebaseStorage;
User({this.profilePathInFirebaseStorage});
factory User.fromFirestore(DocumentSnapshot snapshot) {
return User(profilePathInFirebaseStorage: snapshot.data['imagePath']);
}
}
class ProfileImage extends StatelessWidget {
final String profilePathInFirebaseStorage;
const ProfileImage(
{Key key,
this.profilePathInFirebaseStorage = '',)
: super(key: key);
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: firebase_storage.FirebaseStorage.instance
.ref(profilePathInFirebaseStorage)
.getDownloadURL(), // Can this operation go into User class?
builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
if (!snapshot.hasData) {
return CircularProgressIndicator();
}
return Container(child: NetworkImage(snapshot.data));
});
}
}
【问题讨论】:
-
我不确定我是否理解问题所在。你不能在你的
User类中添加一个返回getDownloadURL()调用结果的方法吗? -
感谢您的评论。我不想每次显示个人资料图片时都打电话给
getDownloadUrl()。它没有效果。所以我希望User实例保留可下载的 URL。我认为应该在初始化用户实例时完成。
标签: firebase flutter dart firebase-storage