Flutter: Effective way of inserting list of data in database

As mentioned by @chunhunghan, you can use batch to insert bulk data.

Here's step by step guideline:

  1. Get ready your json file e.g cities.json (create csv file of data and use csv to json converter like this)
  2. Add cities.json file in your assets directory
  3. Define it in pubspec.yaml like this:

    assets:
     - assets/cities.json
    
  4. Paste this code inside onCreate method of your database class (make sure its after table creation query)

    Batch batch = db.batch();
    
    String citiesJson = await rootBundle.loadString('assets/json/cities.json');
    List citiesList = json.decode(citiesJson);
    
    
    citiesList.forEach((val) {
      //assuming you have 'Cities' class defined
      Cities city = Cities.fromMap(val);
      batch.insert(tblCities, city.toMap());
    });
    
    batch.commit();
    

That's it! :)


There is Batch support
To avoid ping-pong between dart and native code, you can use Batch:

batch = db.batch();
batch.insert('Test', {'name': 'item'});
batch.update('Test', {'name': 'new_item'}, where: 'name = ?', whereArgs: ['item']);
batch.delete('Test', where: 'name = ?', whereArgs: ['item']);
results = await batch.commit();

official example https://github.com/tekartik/sqflite/blob/master/sqflite/example/lib/batch_test_page.dart

In your case, for loop list with batch.insert command, it's easier to maintain
for simplicity syntax, use toMap, example

batch.insert("cities", city.toMap());   

detail https://www.techiediaries.com/flutter-sqlite-crud-tutorial/

If you prefer rawInsert, please reference Insert multiple records in Sqflite