未经手的异常:type' list< dynamic>'不是类型的子类型。 |如何在模型firebase中输入对象阵列

发布于 2025-01-23 11:10:04 字数 1339 浏览 0 评论 0原文

我一直在尝试从Firebase获取数据 因此,实际数据是:

如何获取ImageUrl?

我的尝试是:

class ProductModel {
  String? name;
  String? price;
  String? discountPrice;
  String? discountRate;
  String? category;
  String? description;
  List<imageObject>? image;

  ProductModel(
      {required this.name,
      required this.price,
      this.category,
      this.description,
      this.discountPrice,
      this.discountRate,
      this.image});

  ProductModel.fromMap(Map<String, dynamic> data) {
    
    name = data['name'];
    // image = data['imageUrls'][0]['url']; // To get single image i do this
    image = data['imageUrls']; // but this is not working 
    category = data['category'];
    description = data['Description'];
    price = data['price'];
    discountPrice = data['discountPrice'];
    discountRate = data['discountRate'];
  }
}

class imageObject {
  final String public_id;
  final String url;

  imageObject({
    required this.public_id,
    required this.url,
  });
}

它给出了例外:未经手的例外:键入'list&lt; dynamic&gt;' ”的子类型

不是

product.image![0].url,

类型

的“列表

I have been trying to actually get the data from firebase
So the actual data is :
enter image description here

How to get the imageUrl ?

My Try is :

class ProductModel {
  String? name;
  String? price;
  String? discountPrice;
  String? discountRate;
  String? category;
  String? description;
  List<imageObject>? image;

  ProductModel(
      {required this.name,
      required this.price,
      this.category,
      this.description,
      this.discountPrice,
      this.discountRate,
      this.image});

  ProductModel.fromMap(Map<String, dynamic> data) {
    
    name = data['name'];
    // image = data['imageUrls'][0]['url']; // To get single image i do this
    image = data['imageUrls']; // but this is not working 
    category = data['category'];
    description = data['Description'];
    price = data['price'];
    discountPrice = data['discountPrice'];
    discountRate = data['discountRate'];
  }
}

class imageObject {
  final String public_id;
  final String url;

  imageObject({
    required this.public_id,
    required this.url,
  });
}

It gives exception :Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'List<imageObject>?'

And to access the first image i am doing,

product.image![0].url,

where product is of type ProductModel

But this is not working

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

太阳哥哥 2025-01-30 11:10:04

您还需要对 imageObject JSON数据进行估算。为此,您需要将 factory imageObject.fromjson()构造函数添加到 imageObject 类中,就像您为ProductModel类所做的那样。

这是您需要的代码:

  class ProductModel {
  String? name;
  String? price;
  String? discountPrice;
  String? discountRate;
  String? category;
  String? description;
  List<imageObject>? image;

  ProductModel({
    required this.name,
    required this.price,
    this.category,
    this.description,
    this.discountPrice,
    this.discountRate,
    this.image,
  });

  factory ProductModel.fromJson(Map<String, dynamic> jsonData) => ProductModel(
        name: jsonData['name'] as String?,
        price: jsonData['price'] as String?,
        category: jsonData['category'] as String?,
        description: jsonData['Description'] as String?,
        discountPrice: jsonData['discountPrice'] as String?,
        discountRate: jsonData['discountRate'] as String?,
        
        //Have a good look here to understand how nested list of maps are deserialized

        image: (jsonData['imageUrls'] as List<dynamic>?)
            ?.map((e) => imageObject.fromJson(e as Map<String, dynamic>))
            .toList(),
      );
}

class imageObject {
  final String public_id;
  final String url;

  imageObject({
    required this.public_id,
    required this.url,
  });

  factory imageObject.fromJson(Map<String, dynamic> jsonData) => imageObject(
        public_id: jsonData['public_id'] as String,
        url: jsonData['url'] as String,
      );
}

我们在这里所做的是,将来自ImageUrls键的数据作为列表,并通过ImageObject方法的JSON构造函数映射每个单独的元素。

You need to deserialize the imageObject json data as well. For that you need to add the factory imageObject.fromJson() constructor to your imageObject class just like you did for the ProductModel class.

Here's the code you need:

  class ProductModel {
  String? name;
  String? price;
  String? discountPrice;
  String? discountRate;
  String? category;
  String? description;
  List<imageObject>? image;

  ProductModel({
    required this.name,
    required this.price,
    this.category,
    this.description,
    this.discountPrice,
    this.discountRate,
    this.image,
  });

  factory ProductModel.fromJson(Map<String, dynamic> jsonData) => ProductModel(
        name: jsonData['name'] as String?,
        price: jsonData['price'] as String?,
        category: jsonData['category'] as String?,
        description: jsonData['Description'] as String?,
        discountPrice: jsonData['discountPrice'] as String?,
        discountRate: jsonData['discountRate'] as String?,
        
        //Have a good look here to understand how nested list of maps are deserialized

        image: (jsonData['imageUrls'] as List<dynamic>?)
            ?.map((e) => imageObject.fromJson(e as Map<String, dynamic>))
            .toList(),
      );
}

class imageObject {
  final String public_id;
  final String url;

  imageObject({
    required this.public_id,
    required this.url,
  });

  factory imageObject.fromJson(Map<String, dynamic> jsonData) => imageObject(
        public_id: jsonData['public_id'] as String,
        url: jsonData['url'] as String,
      );
}

What we did here is, take the data from imageUrls key as a List and map every individual element thru the json constructor of the imageObject method.

遗心遗梦遗幸福 2025-01-30 11:10:04

您必须将重新列表映射到ImageObject s列表。

  1. 为您的imageObject类创建图形构造函数
class imageObject {
  final String public_id;
  final String url;

  imageObject({
    required this.public_id,
    required this.url,
  });
  imageObject.fromMap(Map<String, dynamic> map) => imageObject(public_id = map['public_id'], url = map['url'] );
}
  1. 使用以下内容:
 ProductModel.fromMap(Map<String, dynamic> data) {
    
    name = data['name'];
    image = data['imageUrls'].map((map) => imageObject.fromMap(map) ); 
    category = data['category'];
    description = data['Description'];
    price = data['price'];
    discountPrice = data['discountPrice'];
    discountRate = data['discountRate'];
  }

您可能需要添加一些铸件或对先前的代码进行一些修改使其正常工作,但这是一般的想法。

You have to map the recived list to an imageObjects list.

  1. Create a fromMap constructor for your imageObject class
class imageObject {
  final String public_id;
  final String url;

  imageObject({
    required this.public_id,
    required this.url,
  });
  imageObject.fromMap(Map<String, dynamic> map) => imageObject(public_id = map['public_id'], url = map['url'] );
}
  1. Use it something like the following:
 ProductModel.fromMap(Map<String, dynamic> data) {
    
    name = data['name'];
    image = data['imageUrls'].map((map) => imageObject.fromMap(map) ); 
    category = data['category'];
    description = data['Description'];
    price = data['price'];
    discountPrice = data['discountPrice'];
    discountRate = data['discountRate'];
  }

You may need to do add some casting or do some modifications to the previous code make it work, but this is the general idea.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文