Challenge - 5 Problems
SQLite Master with sqflite
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2: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(), ); } }
Attempts:
2 left
💡 Hint
Remember that ':memory:' creates a temporary database but it exists during the app runtime.
✗ Incorrect
The code creates an in-memory database, inserts two names, queries them, and updates the UI. The ListView shows two Text widgets with 'Alice' and 'Bob'.
📝 Syntax
intermediate1: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]);
Attempts:
2 left
💡 Hint
Check the type of whereArgs parameter.
✗ Incorrect
The whereArgs parameter must be a list of arguments, but here it is given as a single int, causing a TypeError.
❓ lifecycle
advanced1: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?
Attempts:
2 left
💡 Hint
Think about the app lifecycle and resource management.
✗ Incorrect
Closing the database too early causes errors. The best practice is to close it when the app or the database provider is disposed to keep it open while needed.
🔧 Debug
advanced2: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);
Attempts:
2 left
💡 Hint
Check how the database file is opened and if it persists data.
✗ Incorrect
If the database is opened fresh each time without a persistent path, data is lost between runs. Using a fixed path ensures data persists.
🧠 Conceptual
expert1:30remaining
Which option correctly describes sqflite transactions?
In sqflite, what is the main benefit of using transactions for multiple database operations?
Attempts:
2 left
💡 Hint
Think about what happens if one operation in a group fails.
✗ Incorrect
Transactions group operations so either all succeed or none do, preventing partial updates and keeping data consistent.