1
votes

I'm trying to upload images to firebase through firebase storage, and then create a document in firestore containing urls of the said uploaded images. For this,i use this function

   void uploadImageAndCreatePage(List<File> imageFile) async {
    var storage = FirebaseStorage.instance;

    for(int j = 0; j<imageFile.length; j++) {
      StorageReference ref = storage.ref().child("photo"+Random().nextInt(3413555).toString()+j.toString());

      StorageUploadTask uploadTask = ref.putFile(imageFile[j]);

    var url = "";

    var dowurl = await (await uploadTask.onComplete).ref.getDownloadURL();

    url = dowurl.toString();

    downUrl.add(url);
    print("url is:" +downUrl.toString());

  }

    for(int i = 0; i<imageFile.length;i++){
      if(i == 0){
        map['ImgUrl'] = downUrl[0];
      }
     map['Img$i'] = downUrl[i+1 < downUrl.length?i+1:i];

    }

  var doc = await _reference.collection('Products').document("${titleController.text}").get();
    doc.reference.setData(map);
    doc.reference.collection('Reviews').document("DummyRev").setData({
      "name":"null",

    });

    print("done");
  }

The code works fine until it gets to the part where i set the data using documentreference.setData, which takes in a map value. I have this map defined as Map<String,String> map = new Map() , but it still says it's not a subtype of _InternalLinkedHashMap.. where am i going wrong with this? i have also tried to use a Map<dynamic,dynamic> and Map<String,dynamic, to no avail.

2

2 Answers

3
votes

Try this

doc.reference.setData(Map<String, dynamic>.from(map));
0
votes

As answered by Josteve, the issue seems to be because the map type just needs to be String,dynamic at runtime, anything else will throw a type error like that, probably has something to with the way the plugin handles the json request. Just cast it using Map.from(yourMap)