0
0
Fluttermobile~20 mins

SQLite with sqflite package in Flutter - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
SQLite Master with sqflite
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What is the output of this Flutter widget using sqflite?
Consider this Flutter widget that fetches and displays a list of names from a SQLite database using sqflite. What will the UI show after the data is loaded?
Flutter
class NameList extends StatefulWidget {
  @override
  _NameListState createState() => _NameListState();
}

class _NameListState extends State<NameList> {
  List<String> names = [];

  @override
  void initState() {
    super.initState();
    loadNames();
  }

  Future<void> loadNames() async {
    final db = await openDatabase(':memory:');
    await db.execute('CREATE TABLE people (id INTEGER PRIMARY KEY, name TEXT)');
    await db.insert('people', {'name': 'Alice'});
    await db.insert('people', {'name': 'Bob'});
    final result = await db.query('people');
    setState(() {
      names = result.map((row) => row['name'] as String).toList();
    });
  }

  @override
  Widget build(BuildContext context) {
    return ListView(
      children: names.map((name) => Text(name)).toList(),
    );
  }
}
AA runtime error because the database is not opened correctly.
BAn empty screen because the database is in memory and cleared immediately.
CA scrollable list showing two Text widgets: 'Alice' and 'Bob'.
DA single Text widget showing 'AliceBob' concatenated.
Attempts:
2 left
💡 Hint
Remember that ':memory:' creates a temporary database but it exists during the app runtime.
📝 Syntax
intermediate
1:30remaining
What error does this sqflite query code produce?
Look at this sqflite query code snippet. What error will it raise?
Flutter
final db = await openDatabase('my.db');
final result = await db.query('users', where: 'id = ?', whereArgs: [5]);
ANo error, query runs successfully.
BRuntimeError because the database file does not exist.
CSyntaxError due to wrong SQL syntax.
DTypeError because whereArgs expects a list but got an int.
Attempts:
2 left
💡 Hint
Check the type of whereArgs parameter.
lifecycle
advanced
1:30remaining
When should you close the sqflite database in a Flutter app?
In a Flutter app using sqflite, when is the best time to close the database connection to avoid resource leaks?
AOnly when the app is terminated or disposed of the database provider.
BImmediately after each query to free resources.
CNever close it; sqflite manages it automatically.
DAfter every screen navigation to a new page.
Attempts:
2 left
💡 Hint
Think about the app lifecycle and resource management.
🔧 Debug
advanced
2:00remaining
Why does this sqflite insert not add data to the table?
This code runs without errors but the data is not saved. Why?
Flutter
final db = await openDatabase(':memory:');
await db.execute('CREATE TABLE IF NOT EXISTS items (id INTEGER PRIMARY KEY, name TEXT)');
await db.insert('items', {'name': 'Item1'});
final count = Sqflite.firstIntValue(await db.rawQuery('SELECT COUNT(*) FROM items'));
print(count);
AThe insert is missing a transaction, so it is rolled back.
BThe database is opened fresh each time, so previous data is lost.
CThe database is opened but not awaited properly, so insert fails silently.
DThe table creation SQL is incorrect, so insert does nothing.
Attempts:
2 left
💡 Hint
Check how the database file is opened and if it persists data.
🧠 Conceptual
expert
1:30remaining
Which option correctly describes sqflite transactions?
In sqflite, what is the main benefit of using transactions for multiple database operations?
ATransactions ensure all operations succeed or all fail, keeping data consistent.
BTransactions speed up queries by caching results automatically.
CTransactions allow running queries in parallel threads safely.
DTransactions automatically backup the database after each operation.
Attempts:
2 left
💡 Hint
Think about what happens if one operation in a group fails.