如何将带有窗口小部件的列表添加到Flutter中的共享首选项?

问题描述 投票:0回答:1

我如何在不使用联机的情况下将小部件数据列表离线保存到用户电话中。我可以使用共享的首选项来保存数据小部件列表还是有其他方法。我实际上在做的是要保存列表并当用户返回我的应用程序时再次显示,但现在该应用程序忘记了带有空列表的数据。

class AlarmData extends ChangeNotifier {
    List<Alarm> _alarms = [
        Alarm(time:'04:05:06'),
        Alarm(time:'04:05:06'),
        Alarm(time:'04:05:06'),
    ];

    UnmodifiableListView<Alarm> get alarms{
        return UnmodifiableListView(_alarms);
    }

    void addAlarm(String newTaskTitle) {
        final task = Alarm(time: newTaskTitle);
        _alarms.add(task);
        notifyListeners();
    }

    void updateAlarm(Alarm task) {
        task.toggleDone();
        notifyListeners();
    }

    void deleteAlarm(Alarm task) {
        _alarms.remove(task);
        notifyListeners();
    }

    int get alarmCount {
        return alarms.length;
    }
}

预先感谢我是扑扑发展的新手。

flutter dart sharedpreferences flutter-layout flutter-widget
1个回答
0
投票

我想到的一种方法是将它们编码为Json并将它们另存为String,无论是sharedPreferences还是应用程序目录中的文件(使用path_provider)

class Alarm{
  final String time;
  Alarm({this.time});

  @override
  toString(){
    return 'time: $time';
  }

  Map<String, dynamic> toJson() => {
    'time': this.time
  };

  factory Alarm.fromJson(Map<String, dynamic> alarm) => Alarm(
    time: alarm["time"],
  );

}

您的class Alarm需要fromJson和toJson才能正确进行编码/解码。我不知道它是否还有其他参数,但是您可以在构造函数和from / to Json中轻松添加它们。

//You could put this method inside AlarmData if you want to save it everytime you update your changeNotifier
Future<void> saveAlarms(List<Alarm> alarms) async{
  //Option 1 using SharedPreferences
  final SharedPreferences preferences = await SharedPreferences.getInstance();
  final String jsonEncoded = json.encode(alarms);
  await preferences.setString('MyAlarms', jsonEncoded);

  //Option 2 saving a json file in the documentsDirectory of the App
  final Directory appDocDir = await getApplicationDocumentsDirectory();
  final file = File('${appDocDir.path}/MyAlarms.json');
  await file.writeAsString(jsonEncoded);
}

Future<List<Alarm>> get retrieveAlarms async{
  //Option 1 using SharedPreferences
  final SharedPreferences preferences = await SharedPreferences.getInstance();
  final String myAlarms = await preferences.getString('MyAlarms');
  if(myAlarms?.isEmpty ?? true) return <Alarm>[]; //check for null or empty values

  final alarms = json.decode(myAlarms) as List;
  return List<Alarm>.from(alarms.map((x) => Alarm.fromJson(x)));

  //Option 2 saving a json file in the documentsDirectory of the App
  final Directory appDocDir = await getApplicationDocumentsDirectory();
  final readFile = File('${appDocDir.path}/MyAlarms.json');
  if(!(await readFile.exists())) return <Alarm>[]; //check if the file exists
  String jsonAlarms = await readFile.readAsString();
  final jResult = jsonDecode(jsonAlarms) as List;

  return List<Alarm>.from(jResult.map((x) => Alarm.fromJson(x)));

}

现在,您的AlarmData需要一个构造函数,您可以在其中为其提供保存的警报列表

class AlarmData extends ChangeNotifier{
  List<Alarm> _alarms;

  AlarmData(this._alarms);
  ...
}

如果您需要在应用程序开始时或在AlarmData之前完成,则可以在主体中完成将来的retrieveAlarms。>

 Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  final myAlarms = await retrieveAlarms;
  runApp(const HomePage(myAlarms)); //if you want to use it as soon as possible
}

如果您不介意使用另一个软件包,我建议您使用Hive,它允许您存储对象和原始值(就像sharedPreferences一样)

@HiveType(typeId: 0)
class Alarms extends HiveObject {

  @HiveField(0)
  String time;
}

Future<void> _initHive() async {
  final appDocumentDir  = await getApplicationDocumentsDirectory();
  Hive.init(appDocumentDir.path);
  Hive.registerAdapter<Alarms>(AlarmsAdapter()); //check the documentation of hive about how to generate this file
  await Hive.openBox<Alarms>('Alarm'); //a box where all your alarms are
}

Future<void> main() async {
    WidgetsFlutterBinding.ensureInitialized();
    await _initHive(); //if you want to use it as soon as possible
    runApp(const HomePage());
}

[我看到您的类扩展了ChangeNotifier,使用Hive时,它具有开箱即用的选项,因此您每次更改(添加或删除警报)时,它都会更新

ValueListenableProvider<Box<Alarm>>.value( //in case you're using provider package
     value: Hive.box<Alarm>('Alarm').listenable(),
),

OR

ValueListenableBuilder(
    valueListenable: Hive.box<Alarm>('Alarm').listenable(), 
    builder: (context) => ...
),
© www.soinside.com 2019 - 2024. All rights reserved.