SQFlite in Flutter - SQL Database For Flutter App
SQFlite in Flutter - SQL Database For Flutter App
dependencies:
sqflite:
import 'dart:convert';
class Note {
final int id;
final String title;
final DateTime dateCreated;
final String content;
Note({
required this.id,
required this.title,
required this.dateCreated,
required this.content,
});
Stay'id': id, Get new articles from All About Flutter | Flutter and Dart delivered straight
in the loop!
'title': title, to your inbox.
'dateCreated': dateCreated.millisecondsSinceEpoch,
'content': content, Continue with Google
};
} More options
import 'package:sqflite/sqflite.dart';
class NotesDatabase {
Subscribe
static final NotesDatabase to our=newsletter
instance NotesDatabase._init();
Stay in the loop! Get new articles from All About Flutter | Flutter and Dart delivered straight
static Database? _database;
NotesDatabase._init(); to your inbox.
Future<Database> get database async {
Continue with Google
if (_database != null) return _database!;
_database = await _initDB('notes.db');
return _database!; More options
}
}
Stay in the loop! Get new articles from All About Flutter | Flutter and Dart delivered straight
final intType = 'INTEGER NOT NULL';
to your inbox.
await db.execute('''
CREATE TABLE notesTable ( Continue with Google
id $idType,
title $textType, More options
dateCreated $textType,
content $textType
)
''');
}
import 'package:aaf_sqflite/note_model.dart';
import 'package:sqflite/sqflite.dart';
class NotesDatabase {
static final NotesDatabase instance = NotesDatabase._init();
static Database? _database;
NotesDatabase._init();
Future<Database> get database async {
if (_database != null) return _database!;
_database = await _initDB('notes.db');
return _database!;
}
await db.execute('''
CREATE TABLE notesTable (
id $idType, Subscribe to our newsletter
Stay intitle Get new articles from All About Flutter | Flutter and Dart delivered straight
the loop!$textType,
dateCreated $intType, to your inbox.
content $textType
) Continue with Google
''');
} More options
Stay loop! Get new articles from All About Flutter | Flutter and Dart delivered straight
in thedb.update(
return
'notesTable', to your inbox.
note.toMap(),
where: 'id = ?', Continue with Google
whereArgs: [note.id],
); More options
}
import 'package:aaf_sqflite/database.dart';
import 'package:aaf_sqflite/view_note.dart';
import 'package:flutter/material.dart';
import 'note_model.dart';
Stay in the loop! Get new articles from All About Flutter | Flutter and Dart delivered straight
const HomeScreen({Key? key}) : super(key: key);
to your inbox.
@override
Continue with Google
State<HomeScreen> createState() => _HomeScreenState();
}
More options
class _HomeScreenState extends State<HomeScreen> {
final notes = <Note>[];
getNotes() async {
final notes = await NotesDatabase.instance.readAll();
setState(() {
this.notes.clear();
this.notes.addAll(notes);
});
}
@override
void initState() {
getNotes();
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Notes App AAF'),
),
floatingActionButton: FloatingActionButton(
onPressed: () async {
Navigator.push(
context,
MaterialPageRoute(builder: (context) {
return const ViewNote(
id: 0,
Subscribe
isEditing: true, to our newsletter
Stay in the loop!);Get new articles from All About Flutter | Flutter and Dart delivered straight
}), to your inbox.
).then((value) => getNotes());
}, Continue with Google
child: const Icon(Icons.add),
), More options
body: ListView.builder(
itemCount: notes.length,
itemBuilder: (context, index) {
final note = notes[index];
return ListTile(
title: Text(note.title),
subtitle: Text(note.dateCreated.toString()),
trailing: IconButton(
icon: const Icon(Icons.delete),
onPressed: () async {
await NotesDatabase.instance.delete(note.id);
getNotes();
},
),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) {
return ViewNote(
id: note.id,
isEditing: false,
);
},
),
).then((value) => getNotes());
},
);
},
), Subscribe to our newsletter
); in the loop! Get new articles from All About Flutter | Flutter and Dart delivered straight
Stay
} to your inbox.
}
Continue with Google
In the view_note.dart, we first check that
if it is editing mode or note. If yes, then More options
first we need to create a note and then
display it as editing mode. Else we will
fetch the note from database directly.
view_note.dart
COPY
import 'package:aaf_sqflite/note_model.dart';
import 'package:flutter/material.dart';
import 'database.dart';
@override
State<ViewNote> createState() => _ViewNoteState();
}
createNote() async {
final note = Note(
title: titleController.text,
dateCreated: DateTime.now(),
content: contentController.text,
id: 0,
);
final id = await NotesDatabase.instance.create(note);
getNote(id);
}
@override
void initState() {
if (widget.isEditing) {
createNote();
} else {
getNote(widget.id);
}
super.initState();
}
@override
void didChangeDependencies() {
setState(() {
isEditing = widget.isEditing;
});
Stay in the loop! Get newid:articles from All About Flutter | Flutter and Dart delivered straight
this.note.id,
to your inbox.
title: titleController.text,
content: contentController.text,
Continue with Google
dateCreated: DateTime.now(),
);
More options
NotesDatabase.instance.update(note);
setState(() {
this.note = note;
});
}
setState(() {
isEditing = !isEditing;
});
},
child: Text(isEditing ? 'Save' : 'Edit'),
)
],
),
),
);
}
}
import 'package:aaf_sqflite/home_screen.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
Subscribe to our
class MyApp extends StatelessWidget { newsletter
Stay in the loop! Get new articles from All About Flutter | Flutter and Dart delivered straight
const MyApp({Key? key}) : super(key: key);
to your inbox.
@override
Widget build(BuildContext context) { Continue with Google
return MaterialApp(
title: 'AAF SQFlite', More options
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const HomeScreen(),
);
}
}
Output
Subscribe to our newsletter
Stay in the loop! Get new articles from All About Flutter | Flutter and Dart delivered straight
to your inbox.
Continue with Google
Subscribe to our newsletter
More options
Read articles from All About Flutter |
Flutter and Dart directly inside your inbox.
Subscribe to the newsletter, and don't
miss out.
Enter your email address SUBSCRIBE
Written by
Manav Sarkar
Follow
Subscribe to our newsletter
Stay in the loop! Get new articles from All About Flutter | Flutter and Dart delivered straight
to your inbox.
Published on
All About Flutter | Flutter andContinue with Google
Dart
More options
AllAboutFlutter provides the best tutorials for
Flutter and Dart. Easy and up-to-date
tutorials for Flutter and Dart. It is a one-stop
website for the Flutter framework.
Follow
MORE ARTICLES
Manav Sarkar
Language Translation using
MLKit in Flutter - Part 2
Language Translation is a difficult task
and training our model for it makes it
even Harder. But Goo…
Manav Sarkar
Implementing Biometric
Authentication in Flutter
Biometric authentication in the Flutter
app allows developers to allow only
authenticated users to a…
©2024 All About Flutter | Flutter and Dart
Archive · Privacy policy · Terms