flutter type ‘_Set>’ is not a subtype of type ‘Map‘ of ‘data’

huangapple go评论60阅读模式
英文:

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 occurs 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) {
    // 从数据中初始化对象的属性
  }

  Map<String, dynamic> toJson() {
    // 将对象转换为 JSON 格式的数据
  }
}

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 service's 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?

这个方法将图像上传到 Firebase 存储,但是 Firestore 中没有数据。从输入服务信息的页面上,将所有值打印到控制台也没有显示错误,所有值都存在。所以,在这里发生了什么,我漏掉了什么吗?

Help appreciated very much!

非常感谢您的帮助!

英文:

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&lt;dynamic&gt;? 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&lt;String, dynamic&gt; data) {
id = data[&#39;id&#39;];
description = data[&#39;description&#39;];
serviceType = data[&#39;serviceType&#39;];
images = List&lt;String&gt;.from(data[&#39;images&#39;] ?? []);
price = data[&#39;price&#39;];
outletId = data[&#39;outletId&#39;];
discount = data[&quot;discount&quot;];
disStartDate = data[&quot;disStartDate&quot;];
disEndDate = data[&quot;disEndDate&quot;];
averageRating = data[&quot;averageRating&quot;];
numberOfRates = data[&quot;numberOfRates&quot;];
isBird = data[&quot;isBird&quot;];
isCat = data[&quot;isCat&quot;];
isDog = data[&quot;isDog&quot;];
isFish = data[&quot;isFish&quot;];
isHamster = data[&quot;isHamster&quot;];
isRabbit = data[&quot;isRabbit&quot;];
isOther = data[&quot;isOther&quot;];
timestamp = data[&quot;timestamp&quot;];
}

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

uploading data is quite basic:

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

modelTreatment.images =
    await Future.wait(images.map((image) =&gt; 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!

答案1

得分: 0

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 {} brackets, you get a set. This is a syntax to define a set.

E.g.:

var s={"apple"};

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

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

英文:

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={&quot;apple&quot;};

Now the s variable type is Set&lt;String&gt; 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>.

huangapple
  • 本文由 发表于 2023年6月6日 01:35:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76408791.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定