0

I want to update my Shared preference so that each time I update my Provider the updated List automatically gets stored in my shared preference. Here is my code.

class AlarmState extends ChangeNotifier {
  var hour = 0;
  var min = 0;
  Set<Weekday> activeDays = Set();
  bool alarmRing = false;
  bool vibrate = false;
  bool snooze = false;
  List<AlarmObject> alarmList = [];

  void update({
    int? hour,
    int? min,
    Set<Weekday>? activeDays,
    bool? alarmRing,
    bool? vibrate,
    bool? snooze,
    List<AlarmObject>? alarmList,
  }) async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    this.hour = hour ?? this.hour;
    this.min = min ?? this.min;
    this.activeDays = activeDays ?? this.activeDays;
    this.alarmRing = alarmRing ?? this.alarmRing;
    this.vibrate = vibrate ?? this.vibrate;
    this.snooze = snooze ?? this.snooze;
    this.alarmList = alarmList ?? this.alarmList;

    if (alarmList == null) {
      return; 
    }
    else{
                preferences.setStringList("alarm_list",
        alarmList.map((e) => jsonEncode(AlarmObject.toMap(e))).toList());
    }
    

    notifyListeners();
  }
}

0 Answers0