如何在Flutter中使用json或其他方式保存我的当前状态 [英] How to save my current state by using json or something else in Flutter

查看:122
本文介绍了如何在Flutter中使用json或其他方式保存我的当前状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何通过修改现有的JSON文件来保存当前状态.但是我找不到省钱的方法.....所以我想问你一个问题.

I want to know how to save my current state by modifying the existing JSON file. but I can't find a way to saving..... So I want to ask you about that.

JSON文件的大小不是很大.因此,在调用加载和保存状态时,我考虑了总计-加载和总计-保存.但是我认为这不是高效且不稳定的...所以我可以寻求帮助吗?

The size of the JSON file is not so big. So I considered total - loading and total - saving when the loading and saving state is called. but I think it is not efficient and unstable... So can I get some help?

推荐答案

请使用此软件包 https://pub .dev/packages/shared_preferences
包装NSUserDefaults(在iOS上)和SharedPreferences(在Android上),为简单数据提供持久存储.数据异步保存到磁盘中

please use this package https://pub.dev/packages/shared_preferences
Wraps NSUserDefaults (on iOS) and SharedPreferences (on Android), providing a persistent store for simple data. Data is persisted to disk asynchronously

示例代码

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

void main() {
  runApp(MaterialApp(
    home: Scaffold(
      body: Center(
      child: RaisedButton(
        onPressed: _incrementCounter,
        child: Text('Increment Counter'),
        ),
      ),
    ),
  ));
}

_incrementCounter() async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  int counter = (prefs.getInt('counter') ?? 0) + 1;
  print('Pressed $counter times.');
  await prefs.setInt('counter', counter);
}

这篇关于如何在Flutter中使用json或其他方式保存我的当前状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆