2

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.

1 Answer 1

2

Indeed nested List or Map are not supported in SQLite, Only simple types are supported (String, num and Uint8List) at the "root" level. You have to "flatten" your model somehow.

For inner objects, You could decide for example to save the Artist details as a JSON text or put each artist field in the Album object. For list, you could as well encode the list as JSON or using a proprietary format.

You can find here an example of solutions for a nested object.

For example, the following simple map is not supported:

{
  "title": "Table",
  "size": {"width": 80, "height": 80}
}

It should be flattened. One solution is to modify the map structure:

CREATE TABLE Product (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  title TEXT,
  width INTEGER,
  height INTEGER)
{"title": "Table", "width": 80, "height": 80}

Another solution is to encoded nested maps and lists as json (or other format), declaring the column as a String.

CREATE TABLE Product (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  title TEXT,
  size TEXT
)

{
  'title': 'Table',
  'size': '{"width":80,"height":80}'
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.