Skip to content
tezvyn:

When to use SQLite over key-value storage?

Source: docs.flutter.devEasyHow cards are made

Tests structured-vs-flat storage judgment. Good answers cite relational data, complex queries, or multi-table schemas, then list adding the dependency, getting a path, opening with onCreate, and keeping a singleton.

What's really being asked

This question checks two things: architectural judgment about local persistence and procedural knowledge of the sqflite plugin. The interviewer wants to see that you do not default to a cannon for a mosquito. You should know when relational semantics, multi-table joins, or ACID transactions justify SQLite over shared_preferences. They also want to hear the exact bootstrap sequence because opening a database incorrectly leads to race conditions, locked files, or lost migrations in production.

The full answer

First, the scenario. Name a case with structured relational data, such as an offline-first task manager with categories, due dates, and assignees, or a cache that must support complex filtering and sorting. Contrast this with key-value storage, which is fine for booleans, theme modes, or auth tokens. Second, the setup steps in order. Add the sqflite and path_provider packages to pubspec.yaml. Resolve the database file path using getDatabasesPath or path_provider so the file lands in the correct platform directory. Call openDatabase with a version integer and an onCreate callback that executes CREATE TABLE statements. Return a singleton or service-wrapped instance so the database is not reopened on every query. Third, mention migrations. A senior candidate notes that version bumps should trigger onUpgrade to alter schema without dropping user data.

The mistakes people make

A red flag is choosing SQLite for a few primitive settings. That adds build size and complexity for no gain. Another red flag is hardcoding a file path like /data/data/myapp/db.db because it breaks on iOS, macOS, and Windows. Opening the database inside a build method or a StatelessWidget is also wrong; it belongs in a repository or service initialized before the app needs it. Finally, omitting the version parameter or running schema setup outside onCreate leads to tables being recreated on every launch or never created at all.

What usually comes next

The interviewer may ask how you handle migrations when the schema changes. They may also ask how you prevent database locks when reading and writing from multiple isolates, or how you unit-test code that depends on sqflite. Be ready to explain that sqflite is not isolate-safe by default and that heavy operations should be batched or run via a background port. They might also ask about encryption, which would push the conversation to sqflite_sqlcipher.

A concrete example

Suppose you are building an offline expense tracker. A key-value store forces you to serialize every receipt into a JSON blob, making it painful to sum amounts by month or category. SQLite fits because you can create an expenses table with amount, category, and date columns, then run SELECT SUM(amount) GROUP BY category. Setup means adding sqflite and path_provider, computing the path with join(await getDatabasesPath(), 'expenses.db'), calling openDatabase with version 1 and onCreate executing CREATE TABLE expenses(id INTEGER PRIMARY KEY, amount REAL, category TEXT, date TEXT), and wrapping the returned Database instance in a singleton repository class.

Interview question

A Flutter app must persist auth tokens and a growing list of categorized tasks with due dates. Which persistence strategy is most appropriate?

  • a.Use SQLite for everything to keep persistence unified and ACID-compliant.
  • b.Use shared_preferences for tokens and SQLite for the tasks.Correct
  • c.Use shared_preferences for all data to avoid extra dependencies.
  • d.Store tasks as JSON in shared_preferences and query them with Dart lists.
Why?

The card states key-value storage is meant for primitives like auth tokens, while structured relational data such as categorized tasks justifies SQLite for complex queries and sorting. Choosing SQLite for simple tokens adds unnecessary build size and complexity, and storing structured data as JSON blobs makes filtering and aggregation painful.

Just read this? Test yourself on what you have been reading.

Read the original → docs.flutter.dev

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on flutter — each one lists the topics its interview covers.

See open roles