如何使图像在添加到firebase后立即显示,而无需重新启动fluter
我的代码是正常的,但是如果我向Firebase添加数据,图像不会显示,但是如果它已重新启动,应用程序会自动显示并且图像成功显示,如何解决它如何使图像在添加到firebase后立即显示,而无需重新启动fluter,firebase,flutter,Firebase,Flutter,我的代码是正常的,但是如果我向Firebase添加数据,图像不会显示,但是如果它已重新启动,应用程序会自动显示并且图像成功显示,如何解决它 child: new Container( child: Image.network( '${doc.data["image"]}' + '?alt=media',), width: 170, height: 120, ), 下面是添加它的代码 void crea
child: new Container(
child: Image.network(
'${doc.data["image"]}' + '?alt=media',),
width: 170,
height: 120,
),
下面是添加它的代码
void createData() async {
DateTime now = DateTime.now();
String format = DateFormat('dd:mm:yy').format(now);
var fullImageName = 'foto-$format' + '.jpg';
var fullImageName2 = 'foto-$format' + '.jpg';
final StorageReference ref =
FirebaseStorage.instance.ref().child(fullImageName);
final StorageUploadTask task = ref.putFile(image);
var part1 =
'https://firebasestorage.googleapis.com/v0/b/db-tubes.appspot.com/o/';
var fullPathImage = part1 + fullImageName2;
print(fullPathImage);
if (_formKey.currentState.validate()) {
_formKey.currentState.save();
DocumentReference ref = await db
.collection('resep')
.add({'nama': '$nama', 'resep': '$resep', 'image': '$fullPathImage', 'email' : widget.email});
setState(() => id = ref.documentID);
Navigator.of(context).pop();
}
}
这只是你想法的一个示范
Future uploadFile() async {
StorageReference storageReference = FirebaseStorage.instance
.ref()
.child('chats/${Path.basename(_image.path)}}');
StorageUploadTask uploadTask = storageReference.putFile(_image);
await uploadTask.onComplete;
print('File Uploaded');
storageReference.getDownloadURL().then((fileURL) {
setState(() {
_uploadedFileURL = fileURL;
});
});
}
在设置状态中,将该URL放入局部变量,并将该局部变量作为网络使用到图像文件中,它将在下一个Dom创建时自动获取。
StorageUploadTask任务是未来的任务,因此您需要等待上载任务完成才能获取URL
试试这个
void createData() async {
DateTime now = DateTime.now();
String format = DateFormat('dd:mm:yy').format(now);
var fullImageName = 'foto-$format' + '.jpg';
var fullImageName2 = 'foto-$format' + '.jpg';
final StorageReference ref =
FirebaseStorage.instance.ref().child(fullImageName);
final StorageUploadTask task = ref.putFile(image);
// Wait upload task to complete
final StorageTaskSnapshot downloadUrl =
(await task.onComplete);
// Get image uRL
final String url = (await downloadUrl.ref.getDownloadURL());
if (_formKey.currentState.validate()) {
_formKey.currentState.save();
DocumentReference ref = await db
.collection('resep')
.add({'nama': '$nama', 'resep': '$resep', 'image': '$url', 'email' : widget.email});
setState(() => id = ref.documentID);
Navigator.of(context).pop();
}
}
}
如果刷新小部件时图像没有更新,我在这里看到一个常见问题,即将文件写入firebase存储中的相同地址/名称。
不知何故,如果文件名与旧文件名相同(如:“2019-11-19-file.jpg”),则图像小部件无法理解图像已被更新。如果你再写一次,它就不会更新了
在这种情况下,您应该使文件名包含一个随机组件,例如UTC毫秒时间戳名称(“1234512312 file.jpg”),或者在图像URL字符串的末尾添加“&random=[在此插入随机数]”。您需要流来进行连续更新。您还没有编写任何与之相关的代码好的,先生,我会先试试。我能收到您的电子邮件吗,我对颤振感到很难过。您能帮我解决您的问题吗?