如何在 Flutter 中使用 SharedPreferences 保存 DateFormat?

How to save DateFormat with SharedPreferences in Flutter?

我试图计算每个小时按下按钮的次数。为此,当我按下按钮时,我将当前时间保存在一个变量中以参考按下按钮的小时数,如果我第二次按下按钮,我想将当前时间与之前保存的时间进行比较知道时间是否改变了。如果有不同的时间,我将计数器重置为 0,否则我增加计数器。这样我就可以知道我每小时按了多少按钮。可能有更简单的解决方案。

我的问题:我尝试用 SharedPreferences 保存日期,然后进行比较,但 return null

import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:intl/intl.dart';
void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(

        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  var currentTime = DateFormat('yyyy-MM-dd – kk:mm').format(DateTime.now());
  var currentTime2 = DateFormat('yyyy-MM-dd – kk:mm').format(DateTime.now());
  int consommation = 0;
  String reftime_string;

//------------------------------------------------------------------------------------------------
  save_refTime() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    setState(() {


      prefs.setString('$currentTime2', reftime_string);
    });
    load_refTime();
  }

  load_refTime() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    setState(() {

      reftime_string = (prefs.getString('$currentTime2'));

    });
  }
//--------------------------------------------------------------------------------------------------------------
  save_data() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    setState(() {

      if (currentTime==reftime_string){
        consommation++;
        prefs.setInt('$currentTime''consommation', consommation);  // creer un currenttime2 ajustable
      }else{
        consommation=0;
        consommation++;
        prefs.setInt('$currentTime''consommation', consommation);  // creer un currenttime2 ajustable
      }

    });
    load_data();
  }

  load_data() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    setState(() {
      consommation = (prefs.getInt('$currentTime''consommation'));

    });
  }


  void _incrementCounter() {
    setState(() {
      save_refTime();
      save_data();
    });
  }

  @override
  void initState() {
    // TODO: implement initState


    super.initState();
  }



  @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(
              '$consommation',
              style: Theme.of(context).textTheme.display1,
            ),
            Text(
              '$currentTime',
              style: Theme.of(context).textTheme.display1,
            ),
            Text(
              '$reftime_string',
              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.
    );
  }
}

SharedPreferences putget 方法需要一个键作为字符串作为其第一个参数,实际值作为第二个参数。键用于映射值,以便可以找到它以进行保存和加载。在您的实现中,密钥根据值进行更改,因此它无法映射到现有数据,您会得到 null.

选择唯一标识符,您的问题应该像这样解决:

节省:prefs.setString('dateTimeLastClicked', currentTime);

正在加载:reftime_string = prefs.getString('dateTimeLastClicked');

此外:我建议您避免使用 var - 如果您使用显式类型,它将对您有很大帮助,这样您就不会因为错误的类型假设等而出现异常!

您可以使用时间戳而不是使用 DateFormat 来保存 date/time 共享首选项。

int timestamp = DateTime.now().millisecondsSinceEpoch;

final prefs = await SharedPreferences.getInstance();
prefs.setInt('myTimestampKey', timestamp);

然后当您想再次取回它时,您可以将时间戳转换回 DateTime 对象。

final prefs = await SharedPreferences.getInstance();
int timestamp = prefs.getInt('myTimestampKey');

DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(timestamp);

你可以得到两个日期之间的差异作为 Duration:

DateTime before = DateTime.fromMillisecondsSinceEpoch(timestamp);
DateTime now = DateTime.now();
Duration timeDifference = now.difference(before);

一旦你有了持续时间,就很容易得到分钟数或小时数或其他什么。

int hours = timeDifference.inHours;

替代方法

最近我一直在使用 ISO 8601 Date and Time Format 作为时间戳。与纪元以来的毫秒数不同,ISO 8601 日期是人类可读的。

日期时间 --> ISO 8601 字符串

String timeStamp = DateTime.now().toIso8601String();
// 2020-04-17T11:59:46.405

ISO 8601 字符串 --> 日期时间

DateTime dateTime = DateTime.parse('2020-04-17T11:59:46.405');

备注

dateTime.toString() 不同,ISO 8601 方法没有任何空格:

2020-04-17T11:59:46.405  // dateTime.toIso8601String()
2020-04-17 11:59:46.405  // dateTime.toString()