• flutter中存储键值对简单数据(相当于前端localstorage概念)


    首先需要安装一个官方推荐包:

    1 dependencies:
    2   flutter:
    3     sdk: flutter
    4   shared_preferences: any
    // 先获取 shared preferences 
    final prefs = await SharedPreferences.getInstance();
    
    // 设置value
    prefs.setInt('counter', counter);

    // 获取value
    final counter = prefs.getInt('counter');

    // 删除value
    prefs.remove('counter');
     

    支持的类型如下:intdoubleboolstring , stringList

    完整例子:

    import 'package:flutter/material.dart';
    import 'package:shared_preferences/shared_preferences.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      // This widget is the root of our application.
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Shared preferences demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: MyHomePage(title: 'Shared preferences demo'),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      MyHomePage({Key key, this.title}) : super(key: key);
      final String title;
    
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      int _counter = 0;
    
      @override
      void initState() {
        super.initState();
        _loadCounter();
      }
    
      //Loading counter value on start
      _loadCounter() async {
        SharedPreferences prefs = await SharedPreferences.getInstance();
        setState(() {
          _counter = (prefs.getInt('counter') ?? 0);
        });
      }
    
      //Incrementing counter after click
      _incrementCounter() async {
        SharedPreferences prefs = await SharedPreferences.getInstance();
        setState(() {
          _counter = (prefs.getInt('counter') ?? 0) + 1;
          prefs.setInt('counter', _counter);
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text(widget.title),
          ),
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Text(
                  'You have pushed the button this many times:',
                ),
                Text(
                  '$_counter',
                  style: Theme.of(context).textTheme.display1,
                ),
              ],
            ),
          ),
          floatingActionButton: FloatingActionButton(
            onPressed: _incrementCounter,
            tooltip: 'Increment',
            child: Icon(Icons.add),
          ), // This trailing comma makes auto-formatting nicer for build methods.
        );
      }
    }
     
  • 相关阅读:
    CodeForces1152CNeko does Maths
    π的计算公式
    IFS变量对加双引号和不加双引号变量的区别对待
    各种距离
    ADO.NET Data Service 二 绑定数据类
    向客户端注入JavaScript代码
    Ajax ModelPopu and Progress 示例学习
    Sliverlight 入门教程七
    (牛人莫入)Jquery plugin 多文件上传
    自定义控件的验证使用
  • 原文地址:https://www.cnblogs.com/pjl43/p/9861316.html
Copyright © 2020-2023  润新知