This commit is contained in:
Vandad Nahavandipoor 2022-01-05 07:04:27 +01:00
parent 79202e0328
commit 94b65f952b
2 changed files with 27 additions and 10 deletions

View File

@ -12,11 +12,16 @@ class NotesService {
List<DatabaseNote> _notes = [];
static final NotesService _shared = NotesService._sharedInstance();
NotesService._sharedInstance();
NotesService._sharedInstance() {
_notesStreamController = StreamController<List<DatabaseNote>>.broadcast(
onListen: () {
_notesStreamController.sink.add(_notes);
},
);
}
factory NotesService() => _shared;
final _notesStreamController =
StreamController<List<DatabaseNote>>.broadcast();
late final StreamController<List<DatabaseNote>> _notesStreamController;
Stream<List<DatabaseNote>> get allNotes => _notesStreamController.stream;

View File

@ -21,12 +21,6 @@ class _NotesViewState extends State<NotesView> {
super.initState();
}
@override
void dispose() {
_notesService.close();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
@ -75,7 +69,25 @@ class _NotesViewState extends State<NotesView> {
switch (snapshot.connectionState) {
case ConnectionState.waiting:
case ConnectionState.active:
return const Text('Waiting for all notes...');
if (snapshot.hasData) {
final allNotes = snapshot.data as List<DatabaseNote>;
return ListView.builder(
itemCount: allNotes.length,
itemBuilder: (context, index) {
final note = allNotes[index];
return ListTile(
title: Text(
note.text,
maxLines: 1,
softWrap: true,
overflow: TextOverflow.ellipsis,
),
);
},
);
} else {
return const CircularProgressIndicator();
}
default:
return const CircularProgressIndicator();
}