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
}
Hey, here is how you could try:
first step, let's create the string list converter look like this, it because the image field is a list of URL strings.
Then, you could implement it in your existing product model like this:
class Products{