Skip to content

How to insert data on first app run? #1404

Answered by simolus3
ivanburlakov asked this question in Q&A
Discussion options

You must be logged in to vote

You can insert data when your database gets created, which should come pretty close. You can use the onCreate callback in your migration behavior to run inserts after creating all tables:

MigrationStrategy(
  onCreate: (m) async {
    await m.createAll(); // create all tables
    await into(myTable).insert(...); // insert on first run.
  }
)

You can call all methods from your database in the migration callback (including things like batches or transactions), so you can just insert data in the create callback.

Another approach would be to create a sqlite3 file containing the initial rows as an asset and then using that instead of creating a new sqlite3 database inside your app. Something like

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@ivanburlakov
Comment options

Answer selected by ivanburlakov
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants