Saving a single model in sqflite is quite easy. I am trying to save the nested Model in sqfilte. Model class, table creation query, and error is explained below. Any help will be appreciated to save such nested models:
Main Model:
@JsonSerializable(explicitToJson: true)
class Album {
String? name;
int? playcount;
String? url;
ArtistDetail? artist;
List<Image>? image;
Album({this.name, this.playcount, this.url, this.artist, this.image});
factory Album.fromJson(Map<String, dynamic> json) =>
_$AlbumFromJson(json);
Map<String, dynamic> toJson() => _$AlbumToJson(this);
}
Sub-model:
@JsonSerializable()
class Image {
dynamic _text;
String? _size;
dynamic get text => _text;
String? get size => _size;
Image({dynamic text, String? size}) {
_text = text;
_size = size;
}
factory Image.fromJson(Map<String, dynamic> json) => _$ImageFromJson(json);
Map<String, dynamic> toJson() => _$ImageToJson(this);
}
Function to store image into sqflite:
// Table creation query
//'CREATE TABLE $TABLE ( $ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, $ALBUM_NAME //TEXT, $PLAY_COUNT INTEGER, $ALBUM_URL TEXT, $ARTIST_DETAIL TEXT, $IMAGES TEXT )'
//Function to perform insertion:
Future<int> insert(Album album) async {
var dbClient = await db;
return await dbClient.insert(
TABLE,
album.toJson(),
conflictAlgorithm: ConflictAlgorithm.replace,
);
}
Error:
Invalid argument [{#text: https:5d72a77281bec2f7ddea87c48.png, size: small}] with type List<Map<String, dynamic>>. The only num, String, and Uint8List are supported.