'Flutter TableCalendar . SharedPreferences

I want to save the table calendar data. First of all, I succeeded in creating data for each date, but I don't know how to use sharedPreferences. I don't know what it is even if I googled it and applied it in various ways. https://medium.flutterdevs.com/display-dynamic-events-at-calendar-in-flutter-22b69b29daf6 I think the code that's closest to the answer is this person's method If you apply it and make some changes, you will encounter various bugs. I spent almost a week on this for three hours. It's so hard that I just want to solve it today. I have to work out, and I have a lot of other assignments Because of the desire to keep solving this problem, it doesn't get out of the chair. My personality is getting dirty after meeting a problem that hasn't been solved for a week. I beg you.

class DiaryService extends ChangeNotifier {
  DateTime focusedDay = DateTime.now();
  DateTime selectedDay = DateTime.now();

  TextEditingController createFieldController = TextEditingController();
  TextEditingController updateFieldController = TextEditingController();

  List<Diary> diaryList = [];

  Map<DateTime, List<Diary>> diaryMap = {};

  List<Diary> getByDate(DateTime date) {
    return diaryMap[date] ?? [];
  }

  void create(String text, DateTime selectedDate) {
    diaryList.add(Diary(text: text, createdAt: DateTime.now(), selectedDate: 
selectedDate));
    diaryMap[selectedDate] = diaryList.where((element) {
      return element.selectedDate == selectedDate;
    }).toList();
    print(selectedDate.day);
    notifyListeners();
  }

  void update(DateTime createdAt, String newContent) {
    int t = diaryList.indexWhere((element) => element.createdAt == createdAt);
    diaryList.removeWhere((element) => element.createdAt == createdAt);
    diaryList.insert(t, Diary(text: newContent, createdAt: createdAt, 
selectedDate: selectedDay));
    addListInMap();
    notifyListeners();
  }

  void delete(DateTime createdAt) {
    diaryList.removeWhere((element) => element.createdAt == createdAt);
    addListInMap();
    print(getByDate);
    print(diaryMap);
    notifyListeners();
  }

  void addListInMap() {
    diaryMap[selectedDay] = diaryList.where((element) => element.selectedDate 
== selectedDay).toList();
  }

  void encodeData() async {
    String dd = Diary.encode(diaryList);
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.setString("diaryList", dd);
    notifyListeners();
  }

  Future<List<Diary>> decodeData() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String getData = prefs.getString("diaryList") ?? "[]";
    print(getData);
    diaryList = Diary.decode(getData);
    notifyListeners();
    return diaryList;
  }
}


Solution 1:[1]

Try installing it with pip again (pip install PyNaCl)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ash