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

Nit*_*euq 5 datetime-format sharedpreferences flutter

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

我的问题:我试图用 SharedPreferences 保存日期,然后进行比较,但它返回 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.
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

Sur*_*gch 22

您可以使用时间戳,而不是使用 DateFormat 在共享首选项中保存日期/时间。

int timestamp = DateTime.now().millisecondsSinceEpoch;

final prefs = await SharedPreferences.getInstance();
prefs.setInt('myTimestampKey', timestamp);
Run Code Online (Sandbox Code Playgroud)

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

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

DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(timestamp);
Run Code Online (Sandbox Code Playgroud)

您可以获得两个日期之间的差异作为持续时间:

DateTime before = DateTime.fromMillisecondsSinceEpoch(timestamp);
DateTime now = DateTime.now();
Duration timeDifference = now.difference(before);
Run Code Online (Sandbox Code Playgroud)

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

int hours = timeDifference.inHours;
Run Code Online (Sandbox Code Playgroud)

替代方法

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

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

String timeStamp = DateTime.now().toIso8601String();
// 2020-04-17T11:59:46.405
Run Code Online (Sandbox Code Playgroud)

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

DateTime dateTime = DateTime.parse('2020-04-17T11:59:46.405');
Run Code Online (Sandbox Code Playgroud)

笔记

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

2020-04-17T11:59:46.405  // dateTime.toIso8601String()
2020-04-17 11:59:46.405  // dateTime.toString()
Run Code Online (Sandbox Code Playgroud)


kou*_*nex 5

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

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

保存: prefs.setString('dateTimeLastClicked', currentTime);

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

另外:我建议您避免使用var- 如果您使用显式类型,它将对您有很大帮助,因此您会因错误的类型假设等而获得更少的异常!