Flutter mobile provider combat - (50) persistent _shared_preferences

When the app turned off. And then into the time, the contents of the shopping cart is still present.

sqflite offer this to operate SQLite database

flutter offers three persistence tool

To learn is today

shared_preferences

There is also a persistence that is: file, in the form of streams to read or some slow

安装shared_preferences

Plug Address:

https://github.com/flutter/plugins/tree/master/packages/shared_preferences

A good start ahead of Japan VPN nodes is relatively fast download

shared_preferences: ^0.5.1

Take the cart ride some of the persistence of CRUD

cart_page.dart

Introducing shared_preferences then stfull create a dynamic widget

The new method setStringList, of course, there are other ways setBool etc.

A method of increasing completed

show persistence method to get the value from the inside, if there is a value to a variable to use setState testList assignment, so the interface will change.

Delete methods, one is all empty, empty one is based on key values

Write build method

At the top of the build before calling the method show,

The following were then ListView layout with the container and put two buttons

Operating results show:

Click the Add button to add a few records

Enter in the terminal is out of the q

flutter run rerun. The original data is still

The final code

cart_page.dart

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

class CartPage extends StatefulWidget {
  @override
  _CartPageState createState() => _CartPageState();
}

class _CartPageState extends State<CartPage> {
  List<String> testList=[];
  @override
  Widget build(BuildContext context) {
    _show();
    return Container(
      child: Column(
        children: <Widget>[
          Container(
            height: 500.0,
            child: ListView.builder(
              itemCount: testList.length,
              itemBuilder: (context,index){
                return ListTile(
                  title: Text(testList[index]),
                );
              },
            ),
          ),
          RaisedButton(
            onPressed: (){_add();},
            child: Text('增加'),
          ),
           RaisedButton(
            onPressed: (){_clear();},
            child: Text('清空'),
          )
        ],
      ),
    );
  }
  //增加方法 
  void _add() async{
    SharedPreferences prefs=await SharedPreferences.getInstance();
    String temp = 'wjw是最棒哒!!!!!';
    testList.add(temp);
    prefs.setStringList('testInfo', testList);
    _show();
  }

   void _show() async{
     SharedPreferences prefs=await SharedPreferences.getInstance();
     if(prefs.getStringList('testInfo')!=null){
       setState(() {
        testList= prefs.getStringList('testInfo');
       });
     }
   }
   //删除

  void _clear() async{
    SharedPreferences prefs=await SharedPreferences.getInstance();
     //prefs.clear();//全部删除
    prefs.remove('testInfo');//根据key值清空
    setState(() {
     testList=[];
    });
   }

}

.

Guess you like

Origin www.cnblogs.com/crazycode2/p/11524488.html
Recommended