Skip to content

Commit 7c5fc22

Browse files
Part 4- Fetch Note
1 parent b5fcf80 commit 7c5fc22

File tree

2 files changed

+58
-4
lines changed

2 files changed

+58
-4
lines changed

lib/database/database.dart

+4
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,8 @@ class AppDatabase extends _$AppDatabase {
3232

3333
@override
3434
int get schemaVersion => 1;
35+
//GET ALL THE NOTES FROM DB
36+
Future<List<NoteData>> getNoteList() async {
37+
return await select(note).get();
38+
}
3539
}

lib/screen/note_list_page.dart

+54-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import 'package:flutter/material.dart';
2+
import 'package:note_keeper/database/database.dart';
3+
import 'package:provider/provider.dart';
24

35
class NoteListPage extends StatefulWidget {
46
const NoteListPage({Key? key}) : super(key: key);
@@ -8,15 +10,63 @@ class NoteListPage extends StatefulWidget {
810
}
911

1012
class _NoteListPageState extends State<NoteListPage> {
13+
late AppDatabase database;
1114
@override
1215
Widget build(BuildContext context) {
16+
database = Provider.of<AppDatabase>(context);
1317
return Scaffold(
14-
body: Center(
15-
child: Text(
16-
'Hello World!',
17-
style: Theme.of(context).textTheme.headline5,
18+
body: FutureBuilder<List<NoteData>>(
19+
future: _getNoteFromDatabase(),
20+
builder: (context, snapshot) {
21+
if (snapshot.hasData) {
22+
List<NoteData>? noteList = snapshot.data;
23+
if (noteList != null) {
24+
if (noteList.isEmpty) {
25+
return Center(
26+
child: Text(
27+
'No Notes Found, Click on add button to add new note',
28+
textAlign: TextAlign.center,
29+
style: Theme.of(context).textTheme.bodyText2,
30+
),
31+
);
32+
} else {
33+
return noteListUI(noteList);
34+
}
35+
}
36+
} else if (snapshot.hasError) {
37+
return Center(
38+
child: Text(
39+
snapshot.error.toString(),
40+
style: Theme.of(context).textTheme.bodyText2,
41+
));
42+
}
43+
return Center(
44+
child: Text(
45+
'Click on add button to add new note',
46+
style: Theme.of(context).textTheme.bodyText2,
47+
),
48+
);
49+
},
50+
),
51+
floatingActionButton: FloatingActionButton(
52+
onPressed: () {},
53+
shape: CircleBorder(
54+
side: BorderSide(color: Colors.black, width: 2),
55+
),
56+
backgroundColor: Colors.white,
57+
child: Icon(
58+
Icons.add,
59+
color: Colors.black,
1860
),
1961
),
2062
);
2163
}
64+
65+
Future<List<NoteData>> _getNoteFromDatabase() async {
66+
return await database.getNoteList();
67+
}
68+
69+
Widget noteListUI(List<NoteData> noteList) {
70+
return Container();
71+
}
2272
}

0 commit comments

Comments
 (0)