c语言怎样使用文件来存储键值对,flutter中存储键值对简单数据(相当于前端localstorage概念)...

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) {returnMaterialApp(

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{int _counter = 0;

@overridevoidinitState() {

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) {returnScaffold(

appBar: AppBar(

title: Text(widget.title),

),

body: Center(

child: Column(

mainAxisAlignment: MainAxisAlignment.center,

children:[

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.

);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值