0

我想更新我的共享首选项,以便每次更新我的提供者时,更新的列表都会自动存储在我的共享首选项中。这是我的代码。

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();
  }
}
4

0 回答 0