Issue
I have a screen called Upload where a user can upload a post by entering a description and multiple images, and on the homepage I will retrieve data from Firestore.
The problem is, in the upload screen, when I press to upload images and their URL into Firebase storage, there is no problem in storing multiple images, but there is a problem storing the URL in Firestore.
I tried:
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);
});
}
Solution
After getting the url
, you can store in firestore:
var url = downUrl.toString();
print(url);
Firestore.instance.collection("Posts").document("doc_id")setData(
{
"image Urls" : url,
},merge : true).then((_){
print("success!");
});
This will add the url
to the document
Answered By - Peter Haddad
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.