How to store list<String> Data using floor in Flutter

829 views Asked by At

Requirement is to save the API response and show when the device is offline. I have got the JSON data but while using floor not able to find out how can I handle the inner string array data to save it into database. Not able to find how I can use @Typeconverter like we use in ROOM db for kotlin. I am using floor DB for saving the data. While I am running the command flutter packages pub run build_runner build then getting error like Column type is not supported for List?

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
}
1

There are 1 answers

3
Dewa Prabawa On

Hey, here is how you could try:

  1. first step, let's create the string list converter look like this, it because the image field is a list of URL strings.

    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. Then, you could implement it in your existing product model like this:

    class Products{

       @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
           });
    
       // ...
     }