Search code examples
fluttergoogle-cloud-firestoretypeerror

flutter type '_Set<Map<String, dynamic>>' is not a subtype of type 'Map<String, dynamic>' of 'data'


I have two classes to add products and to add services. Both basically the same with minor differences. However, I am getting the above error when I am trying to add services. Adding products works fine. I cannot figure where the error occures and why?

My model class:

class ModelTreatment {
String? id, description, serviceType, outletId, disStartDate, disEndDate;
List<dynamic>? images;
num? price, discount, averageRating, numberOfRates;
bool? isDog, isCat, isHamster, isFish, isBird, isRabbit, isOther;
Timestamp? timestamp;

ModelTreatment({
this.id,
this.description,
this.serviceType,
this.images,
this.price,
this.outletId,
this.discount,
this.disStartDate,
this.disEndDate,
this.averageRating,
this.numberOfRates,
this.isBird, this.isCat, this.isDog, this.isFish, this.isHamster,
this.isRabbit, this.isOther, this.timestamp});

ModelTreatment.fromJson(Map<String, dynamic> data) {
id = data['id'];
description = data['description'];
serviceType = data['serviceType'];
images = List<String>.from(data['images'] ?? []);
price = data['price'];
outletId = data['outletId'];
discount = data["discount"];
disStartDate = data["disStartDate"];
disEndDate = data["disEndDate"];
averageRating = data["averageRating"];
numberOfRates = data["numberOfRates"];
isBird = data["isBird"];
isCat = data["isCat"];
isDog = data["isDog"];
isFish = data["isFish"];
isHamster = data["isHamster"];
isRabbit = data["isRabbit"];
isOther = data["isOther"];
timestamp = data["timestamp"];
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
data["id"] = id;
data["description"] = description;
data["serviceType"] = serviceType;
data["images"] = images;
data["price"] = price;
data["outletId"] = FirebaseAuth.instance.currentUser!.uid;
data["discount"] = discount ?? 0;
data["disStartDate"] = disStartDate ?? '';
data["disEndDate"] = disEndDate ?? '';
data["averageRating"] = averageRating ?? 0;
data["numberOfRates"] = numberOfRates ?? 0;
data["isBird"] = isBird ?? false;
data["isCat"] = isCat ?? false;
data["isDog"] = isDog ?? false;
data["isFish"] = isFish ?? false;
data["isHamster"] = isHamster ?? false;
data["isRabbit"] = isRabbit ?? false;
data["isOther"] = isOther ?? false;
data["timestamp"] = Timestamp.now().toDate();
return data;
 }
}

uploading data is quite basic:

Future<bool> addService({required ModelTreatment modelTreatment, required List<XFile> images})
async {
final ref = serviceCollection.doc();
modelTreatment.id = ref.id;

modelTreatment.images =
    await Future.wait(images.map((image) => uploadFileS(image)));

await ref.set({modelTreatment.toJson()});
return true;
}

the method uploads the images to the firebase storage, however no data in the firestore. From the page, where the services information is entered printing all values to console, also shows no errors, all values are present. So, what is going on here and what am I missing?

Help appreciated very much!


Solution

  • The problem is here:

    await ref.set({modelTreatment.toJson()});
    

    You put your map in a set with the {} brackets.

    Just remove the "{}" brackets.

    Detailed answer:

    If you put a variable in a {} bracket you get a set. This is a syntax to define a set.

    E.g.:

    var s={"apple"};
    

    Now the s variable type is Set<String> with a length of 1.

    If you put the return of "toJson()" which is a Map<String,dynamic> inside a {}, you get a Set<Map<String,dynamic>> which is in the error description, because FirebaseDocument set() method only accept Map<String,dynamic>.