我有一个名为上传的屏幕,用户可以通过输入说明和多张图片来上传帖子。然后在主页上,我将从Firestore检索数据。
问题:
在上载屏幕中,当我按下以在Firebase存储中上传图像以及这些图像在Firestore中的URL时。存储多个图像没有问题。在Firestore中存储URL时出现问题。
我尝试过的:
Widget postButton(){
return RaisedButton(
shape:RoundedRectangleBorder( borderRadius: BorderRadius.circular(15.0),),
color: Color.fromRGBO(0,21,43,1),
onPressed: () {
uploadToFirebase();
UploadPost();
},
textColor: Colors.white,
padding: EdgeInsets.symmetric(vertical: 16.0, horizontal: 24.0),
child: Container(
alignment: Alignment.center,
width: _width/1.7,
child: Text(
"Upload Post",
style: TextStyle(
fontSize: 16.0,
),
),
),
);
}
void UploadPost() async{
UserManagement().addPost(Name,Des,UserImageUrl,ImageUrl,PhoneNumber, context);
}
void uploadToFirebase() async{
_paths.forEach((fileName, filePath) => {upload(fileName, filePath)});
}
void upload(fileName, filePath) async{
DateTime date = new DateTime.now();
var Date = DateFormat('EEE d MMM kk:mm:ss').format(date);
StorageReference storageRef = FirebaseStorage.instance.ref().child("User Posts").child('$PhoneNumber').child('$Date').child('$date');
final StorageUploadTask uploadTask = storageRef.putFile(File(filePath));
var downUrl = await (await uploadTask.onComplete).ref.getDownloadURL();
var url = downUrl.toString();
print(url);
setState(() {
ImageUrl = url;
print("Done");
print(ImageUrl);
});
}
谢谢
获得url
后,您可以将其存储在firestore中:
var url = downUrl.toString();
print(url);
Firestore.instance.collection("Posts").document("doc_id")setData(
{
"image Urls" : url,
},merge : true).then((_){
print("success!");
});
这会将url
添加到文档中