使用提供程序包 FLUTTER 在应用程序关闭后保存状态

cre*_*ate 8 state dart flutter

使用 Flutter 提供程序包保存活动页面状态的最佳方法是什么?目前,使用提供程序包并重新启动或关闭应用程序,页面会重新构建并删除所有新小部件。状态在热重载而不是热重启时保存。例如,当我关闭应用程序或重新启动它时,用户在新闻提要页面上创建的帖子会被删除。

Ada*_*dam 4

因此,根据评论并阅读flutter persistence Cookbook,我看到了三种常见的可能性:

  • 使用 SQLite 保存数据
  • 读写文件
  • 将键值数据存储在磁盘上

每个都有不同的用例,但我认为常见的情况是将一些对象存储为 JSON。在我看来,SQLite 在简单的情况下显得有些过分,而且键值不能提供足够的灵活性。所以我会重点关注文件的读写

这里有一个例子。我使用 JSON 代码生成库,如此处所述

我读取和写入磁盘的数据对象如下所示:

import 'package:json_annotation/json_annotation.dart';

// json methods generated using code geneartion
// see https://flutter.dev/docs/development/data-and-backend/json#serializing-json-using-code-generation-libraries
part 'easy_data.g.dart';

@JsonSerializable()
class EasyData {
    int counter;
    String data;

    EasyData(this.counter, this.data);

  // run the following command to generate json:
  // flutter pub run build_runner build
  factory EasyData.fromJson(Map<String, dynamic> json) => _$EasyDataFromJson(json);

  Map<String, dynamic> toJson() => _$EasyDataToJson(this);
}
Run Code Online (Sandbox Code Playgroud)

从磁盘加载 json 并保存的服务如下所示:

import 'dart:async';
import 'dart:convert';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';

import '../model/easy_data.dart';

class FileStorage with ChangeNotifier {
  EasyData loadedData;

  Future<String> get _localPath async {
    final directory = await getApplicationDocumentsDirectory();
    return directory.path;
  }

  Future<File> get _localFile async {
    final path = await _localPath;
    return File('$path/app_data.json');
  }

  loadData() async {
    try {
      final file = await _localFile;
      // Read the file
      String contents = await file.readAsString();
      if (contents != null) {
        // parse json if file was saved before
        loadedData = EasyData.fromJson(json.decode(contents));
        notifyListeners();
      }
    } on FileSystemException catch (_) {
      // the file did not exist before
    } catch (e) {
      // error handling
      log(e);
    }
  }

  Future<File> writeData(EasyData data) async {
    final file = await _localFile;
    // Write the file
    return file.writeAsString(jsonEncode(data.toJson()));
  }
}
Run Code Online (Sandbox Code Playgroud)

我使用了该服务的提供程序,因为我希望在从磁盘加载数据时通知其他服务。其他 Provider 需要在 main dart 中使用 ChangeNotifierProxyProvider 进行声明,才能使用来自 FileStorage Provider 的信息。

我希望这对您有所帮助。