tekartik / sqflite

SQLite flutter plugin
BSD 2-Clause "Simplified" License
2.86k stars 521 forks source link

Insertion error while saving nested model #675

Open muhammadmateen027 opened 3 years ago

muhammadmateen027 commented 3 years ago

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 {name: Akon, url: https://music/Akon} with type _InternalLinkedHashMap<String, dynamic>.
Only num, String, and Uint8List are supported.

Invalid argument [{#text: https:5d72a77281bec2f7ddea87c48.png, size: small}] with 
type List<Map<String, dynamic>>. The only num, String, and Uint8List are supported.

I have posted the question on stackoverflow https://stackoverflow.com/q/68471462/12932518

stephenhauck commented 9 months ago

SQLite is a relational database and not an object database like Hive or Isar ....