-2

I am using sqflite package to store data offline as well once it got from API. I had seen a couple of examples and all used the simple Strings and not lists in their models as a type and due to that I am feeling difficulty in the conversion of these classes in respective tables and link them so that data can be fetch easily once synced. Can someone guide me on how to make create and link tables in sqflite with these types of classes so that they work perfectly?

TodayDeliveriesModel class

class TodayDeliveriesModel {
  final String Name;
  final List<ItemsModel> allItems;
  final bool pending;

  TodayDeliveriesModel({this.Name, this.allItems, this.pending});

}

Items Model class:

class ItemsModel {
   String name;
   String quantity;

   ItemsModel({this.name, this.quantity});

}
S. Hesam
  • 5,266
  • 3
  • 37
  • 59
Umair
  • 1,759
  • 6
  • 23
  • 44

1 Answers1

0
  • fetch and Parse JSON data from the server.

  • Create Database Table with just 3 parameters.

      class DeliveryModel{
           String name;
             String items;
             bool isPending;
    

    }

  1. then when you save data in the database, just covert List to String
  2. and save this string as the items.

After then when you get this string from database convert this into again List

Deepak Ror
  • 2,084
  • 2
  • 20
  • 26
  • Did i have to create two separate tables for each class or one table is enough for it? – Umair Jun 20 '20 at 09:54
  • 1
    only one table, Just save items list as a string in database, and get it convert it as list. – Deepak Ror Jun 20 '20 at 09:55
  • db.execute("CREATE TABLE $tableName($Column_id INTEGER PRIMARY KEY AUTOINCREMENT, $Column_items TEXT, $Column_name TEXT, $Column_isPending BOOLEAN,)") Is this query correct to create table? – Umair Jun 20 '20 at 10:00
  • Yes, Correct:: Just like Future _onCreate(Database db, int version) async { await db.execute(''' CREATE TABLE $table ( $_notificationID INTEGER PRIMARY KEY, $_infoType TEXT, $_notificationType TEXT, $_sentAT TEXT, $_text TEXT''); } – Deepak Ror Jun 20 '20 at 10:05