如何存储列表<String>Flutter中使用floor的数据

问题描述 投票:0回答:2

要求是保存API响应并在设备离线时显示。我已经获得了 JSON 数据,但在使用 Floor 时无法找出如何处理内部字符串数组数据以将其保存到数据库中。无法找到如何使用 @Typeconverter 就像我们在 ROOM db for kotlin 中使用的那样。我正在使用 Floor DB 来保存数据。当我运行命令时 flutter packages pub run build_runner build然后得到类似错误 列表不支持列类型?

class ProductsModel {
  List<Products>? products;
  int? total;
  int? skip;
  int? limit;

  ProductsModel({this.products, this.total, this.skip, this.limit});

  ProductsModel.fromJson(Map<String, dynamic> json) {
    if (json['products'] != null) {
      products = <Products>[];
      json['products'].forEach((v) {
        products!.add(new Products.fromJson(v));
      });
    }
    total = json['total'];
    skip = json['skip'];
    limit = json['limit'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.products != null) {
      data['products'] = this.products!.map((v) => v.toJson()).toList();
    }
    data['total'] = this.total;
    data['skip'] = this.skip;
    data['limit'] = this.limit;
    return data;
  }
}

@Entity(tableName: articlesTableName)
class Products{
  @primaryKey
  int? id;
  String? title;
  String? description;
  int? price;
  double? discountPercentage;
  double? rating;
  int? stock;
  String? brand;
  String? category;
  String? thumbnail;
  **List<String>? images;   /// HOW CAN I HANDLE THIS**

  Products(
      {
        this.id,
        this.title,
        this.description,
        this.price,
        this.discountPercentage,
        this.rating,
        this.stock,
        this.brand,
        this.category,
        this.thumbnail,
        this.images});

  Products.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    title = json['title'];
    description = json['description'];
    price = json['price'];
    discountPercentage = json['discountPercentage'] == null ? 0.0 : json['discountPercentage'].toDouble();
    rating = json['rating'] == null ? 0.0 : json['rating'].toDouble();
    stock = json['stock'];
    brand = json['brand'];
    category = json['category'];
    thumbnail = json['thumbnail'];
    images = json['images'].cast<String>();
  }


  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['title'] = this.title;
    data['description'] = this.description;
    data['price'] = this.price;
    data['discountPercentage'] = this.discountPercentage;
    data['rating'] = this.rating;
    data['stock'] = this.stock;
    data['brand'] = this.brand;
    data['category'] = this.category;
    data['thumbnail'] = this.thumbnail;
    data['images'] = this.images;
    return data;
  }
}

JSON

{
  "products": [
    {
      "id": 1,
      "title": "iPhone 9",
      "description": "An apple mobile which is nothing like apple",
      "price": 549,
      "discountPercentage": 12.96,
      "rating": 4.69,
      "stock": 94,
      "brand": "Apple",
      "category": "smartphones",
      "thumbnail": "https://i.dummyjson.com/data/products/1/thumbnail.jpg",
      "images": [
        "https://i.dummyjson.com/data/products/1/1.jpg",
        "https://i.dummyjson.com/data/products/1/2.jpg",
        "https://i.dummyjson.com/data/products/1/3.jpg",
        "https://i.dummyjson.com/data/products/1/4.jpg",
        "https://i.dummyjson.com/data/products/1/thumbnail.jpg"
      ]
    }
  ],
  "total": 100,
  "skip": 0,
  "limit": 30
}
json flutter flutter-floor
2个回答
0
投票

嘿,您可以尝试以下方法:

  1. 第一步,让我们创建如下所示的字符串列表转换器,因为图像字段是 URL 字符串列表。

    class StringListConverter extends TypeConverter<List<String>,List<dynamic>> {
       @override
       List<String> fromJson(List<dynamic> json) {
         return json.cast<String>();
       }
    
       @override
       List<dynamic> toJson(List<String> object) {
         return object.cast<dynamic>();
       }
     }
    
  2. 然后,您可以在现有的产品模型中实现它,如下所示:

    类产品{

       @primaryKey
       int? id;
       String? title;
       String? description;
       int? price;
       double? discountPercentage;
       double? rating;
       int? stock;
       String? brand;
       String? category;
       String? thumbnail;
    
       @TypeConverters(StringListConverter)
       List<String>? images;
    
       Products(
           { this.id, this.title, this.description, this.price,
             this.discountPercentage, this.rating, this.stock,
             this.brand, this.category, this.thumbnail,this.images
           });
    
       // ...
     }
    

0
投票

您需要使

List<String>
String
可以为空

示例:

class StringListConverter extends TypeConverter<List<String>?, String?> {
  @override
  List<String>? decode(String? databaseValue) {
    return (databaseValue?.split(","));
  }

  @override
  String? encode(List<String>? value) {
    return value?.join(",");
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.