Flutter Future Asynchronous Processing Does Not Work in the Order You Intended

Asked 3 months ago, Updated 3 months ago, 11 views

I would like to use Flutter to upload the image to Firebase's FireStorage and save the image URL to Forest.

We upload images of Firestorage through asynchronous processing.
However, during the upload, the Firestore is saved and the image URL is not saved.

  • Flutter:version 1.20.2
  • Dart:version 2.9.1

pubspec.yaml

  • firebase_core:^0.5.0
  • cloud_firestore:^0.14.0+2
  • firebase_storage:^4.0.0
Future<void>saveData(
      String title, String address, String description) async {
    
   /// ↑ Retrieve the form information entered as an argument
  
    String_userimageurl,_displayname,_username,_uid;
    _uid = FirebaseAuth.instance.currentUser.uid;


    // Get user information from DB
    var result=wait FirebaseFirestore.instance
        .collection('users')
        .where("uid", isEqualTo:_uid)
        .get();

    _userimageurl=result.docs[0].get("imageurl");
    _displayname=result.docs[0].get("displayname");
    _username=result.docs[0].get("username");


    // Image upload, URL acquisition
    value = wait this.saveImage(this.images);

   // Save Firestore
    waitFirebaseFirestore.instance.collection('posts').add({
      'title': title,
      'address': address,
      'description': description,
      'star' —This.star_cnt,
      "category"—this.category,
      "images": value,
      "user_id":_uid,
      "username":_username,
      "user_displayname":_displayname,
      "user_imageurl":_userimageurl,
      'created': new DateTime.now(),
    });

    notifyListeners();
  }

Future<List<String>>saveImage(List<Asset>images)async{
    List<String>images_arr=[];
    wait images.forEach(element)async {
      String unixtimestamp=
          DateTime.now().toUtc().millisecondsSinceEpoch.toString();
      ByteData byteData=wait element.getByteData();
      List<int>imageData=byteData.buffer.asUint8List();
      StorageReference ref=FirebaseStorage.instance
          .ref()
          .child("images")
          .child("${unixtimestamp}.jpeg");

      StorageUploadTask UploadTask=ref.putData(
          imageData,
          StorageMetadata(
            contentType: "image/jpeg",
          ));

      varurl = wait (wait uploadTask.onComplete).ref.getDownloadURL();

      images_arr.add(url);
    });
    return images_arr;
  }

Note: multi_image_picker FireStore Sample Code

Code Supplement

Uploading images, obtaining download URLs, and saving DBs are already done.
All you can do is wait until you get the URL of the image and save the DB.

I think Future and wait are wrong, but I asked because it didn't solve the problem after reading the online article.
Thank you for your cooperation.

firebase flutter dart

2022-09-30 11:17

1 Answers

Future.forEach seems to solve this problem.

wait images.forEach(element)async{

waitFuture.forEach(images, (element)async{

https://stackoverflow.com/questions/42467663/async-await-in-list-foreach


2022-09-30 11:17

If you have any answers or tips


© 2023 OneMinuteCode. All rights reserved.