import 'package:refilc/api/providers/user_provider.dart'; import 'package:refilc/api/providers/database_provider.dart'; import 'package:refilc/models/self_note.dart'; import 'package:refilc/models/user.dart'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; class SelfNoteProvider with ChangeNotifier { late List _notes; late BuildContext _context; List get notes => _notes; SelfNoteProvider({ List initialNotes = const [], required BuildContext context, }) { _notes = List.castFrom(initialNotes); _context = context; if (_notes.isEmpty) restore(); } // restore self notes from db Future restore() async { String? userId = Provider.of(_context, listen: false).id; // await Provider.of(_context, listen: false) // .userStore // .storeSelfNotes([], userId: userId!); // load self notes from db if (userId != null) { var dbNotes = await Provider.of(_context, listen: false) .userQuery .getSelfNotes(userId: userId); _notes = dbNotes; notifyListeners(); } } // fetches fresh data from api (not needed, cuz no api for that) // Future fetch() async { // } // store self notes in db Future store(List notes) async { User? user = Provider.of(_context, listen: false).user; if (user == null) throw "Cannot store Self Notes for User null"; String userId = user.id; await Provider.of(_context, listen: false) .userStore .storeSelfNotes(notes, userId: userId); _notes = notes; notifyListeners(); } }