CodeBlanks Logo Icon CodeBlanks

Challenge 3

Build a notes app with sqflite to add and list notes.

Note app with sqflite

In this challenge you build a note app with sqflite. You open the database with openDatabase, create a notes table with CREATE TABLE, add a new note with insert, and fetch all notes with query. Since each query returns a Future, the functions are async.

final db = await openDatabase('notlar.db');
await db.insert('notlar', {'baslik': 'Alışveriş', 'icerik': 'Süt'});
final liste = await db.query('notlar');

This structure provides persistent and queryable storage.

Practice The notEkle function

Write an async function named notEkle. Open the database with openDatabase('notlar.db') and add a new note with db.insert('notlar', {'baslik': baslik, 'icerik': icerik}). Print 'hazir' in main.

Starter code

main.dart
import 'package:sqflite/sqflite.dart';

// notEkle fonksiyonunu yaz
// openDatabase ile veritabanını aç
// db.insert ile yeni not ekle

void main() {
  // 'hazir' yazdır
}

Sample solution

main.dart
import 'package:sqflite/sqflite.dart';

Future<void> notEkle(String baslik, String icerik) async {
  final db = await openDatabase('notlar.db');
  await db.insert('notlar', {'baslik': baslik, 'icerik': icerik});
}

void main() {
  print('hazir');
}

Expected output

hazir

Practice The notlariGetir function

Write an async function named notlariGetir. Open the database with openDatabase('notlar.db'), fetch all notes with db.query('notlar'), and return them. Print 'hazir' in main.

Starter code

main.dart
import 'package:sqflite/sqflite.dart';

// notlariGetir fonksiyonunu yaz
// openDatabase ile veritabanını aç
// db.query ile tüm notları çek
// sonucu döndür

void main() {
  // 'hazir' yazdır
}

Sample solution

main.dart
import 'package:sqflite/sqflite.dart';

Future<List<Map<String, Object?>>> notlariGetir() async {
  final db = await openDatabase('notlar.db');
  return db.query('notlar');
}

void main() {
  print('hazir');
}

Expected output

hazir

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which method is used to add a new record in sqflite?

  • db.insert Correct answer
  • db.update
  • db.query
  • db.delete

Explanation: db.insert adds a new record. db.query reads records, db.delete deletes a record, and db.update updates an existing record; none of these add a new record.

2 Fetch all notes

Question
final db = await openDatabase('notlar.db');
final liste = await db.____('notlar');
Answer
final db = await openDatabase('notlar.db');
final liste = await db.query('notlar');

Explanation: To fetch all records you use db.query. insert adds a new record, delete removes one, and there is no method called create (CREATE TABLE is done with raw SQL).

3 sqflite parts

Term Meaning
openDatabase Opening the database
CREATE TABLE Creating a table
db.insert Adding a record
db.query Fetching records

All lessons in Intermediate