I am adding a file to firebase storage . After obtaining the firebase url of the image , I use setState to set it equal to another string( fileUrl )and then adding it to Firestore database . The problem is its not updating to the firebase url and the String fileUrl still has its initialized value . The way I use to add the file and adding it to firestore database , Im not sure how to fix .
First I select a file :
...
String fileUrl = 'temp';
...
Future uploadFile() async {
if (file == null) return Container();
final fileName = basename(file!.path);
final destination = 'files/$fileName';
task = FirebaseApi.uploadFile(destination, file!);
if (task == null) return;
final shot = await task!.whenComplete(() {});
final urlDownload = await shot.ref.getDownloadURL();
print('Download-Link: $urlDownload'); // This prints out the correct url
setState(() {
fileUrl = urlDownload.toString();
});
}
Then I upload to firebase database :
void postToFirebase(school, String fileName) {
print('fileUrl') ; //But when i check it here it still prints out 'temp'
FirebaseFirestore.instance
.collection("collection_name")
.doc(doc_name)
.collection("collection_name")
.add({
"attachmentUrl": fileUrl,
"attachment_name": fileName,
})
Then my Button (on pressed )
onPressed: () async {
uploadFile();
postToFirebase(school, fileName);
}
Not sure how to fix . Any help be much appreciated , thank you
CodePudding user response:
The fileUrl
field was updated in uploadFile
method in an async way. So you are not guaranteed the field will be updated before the following method is called, which is the postToFirebase
.
If you want to make it work sequentially, you can use promise/future to tune the process. New a promise, and complete it in the setState callback, and thus make uploadFile
method return a future depended on the completion of the promise in setState callback method. And then chain the postToFirebase
with the former one using future's then
API.
Code example here:
String fileUrl = "";
Future updateFile() {
Completer completer = Completer();
setState(() {
fileUrl = "new";
completer.complete();
});
return completer.future;
}
void postToFireBase() {
// use the fileUrl updated here
}
void text() {
updateFile().then((value) => postToFireBase());
}