5

I want to save my date thats picked from a DateTime-picker in a shared preferences so it will also be showen after restart the app

child: InkWell(
                      onTap: (){
                        DatePicker.showDateTimePicker(context,
                        showTitleActions: true,
                        minTime: DateTime.now().toLocal(),
                        onConfirm: (date){
                          setState(() {
                            _dateTime = date;

                          });
                        },
                        currentTime: DateTime.now().toLocal(),
                        locale: LocaleType.de
                        );
                      },
                      child: Text(
                        (_dateTime == null ? 'Wählen sie Ihren nächsten Termin' : DateFormat("dd-MM-yyyy hh:mm").format(_dateTime)),
                        style: TextStyle(
                          fontSize: data.size.height / 29,
                          color: Colors.grey[400],
                        ),
                      ),
                    ),
Teepause
  • 51
  • 1
  • 2

3 Answers3

7

convert it to a string & save it

prefs.setString('dateTimeString', _dateTime.toIso8601String());

then use DateTime.parse() to retrieve it:

DateTime _dateTime = DateTime.parse(prefs.getString('dateTimeString'));
Badjio
  • 610
  • 1
  • 8
  • 12
  • do i neet to make a function or can i use it directly in the onTap widget or rather in the text widget? Sorry but im new to flutter – Teepause Mar 17 '20 at 08:35
1

Also you could take a look at the Flutter Secure Storage package. Here: https://pub.dev/packages/flutter_secure_storage I have used it in my projects and it works like a charm. Its as simple as importing the package and doing this:

import 'package:flutter_secure_storage/flutter_secure_storage.dart';

// Create storage
final storage = new FlutterSecureStorage();

// Read value 
String value = await storage.read(key: key);

// Read all values
Map<String, String> allValues = await storage.readAll();

// Delete value 
await storage.delete(key: key);

// Delete all 
await storage.deleteAll();

// Write value 
await storage.write(key: key, value: value);
Pedro R.
  • 643
  • 6
  • 10
  • is there anywehre an example for this im not 100% shure how i have to use it. – Teepause Mar 17 '20 at 11:03
  • Go to the url of the package: https://pub.dev/packages/flutter_secure_storage. And then go to the example tab. Its very simple to install and use. And its encrypted as a bonus. – Pedro R. Mar 17 '20 at 14:13
0

Now i did this:

FlutterSecureStorage storage;

  @override
    void initState() {
      super.initState();
      storage = FlutterSecureStorage();
    }

  @override
    void dispose() {
      super.dispose();
      storage = null;
    }


  String testa;

and used it like this:

child: InkWell(
                          onTap: (){
                            DatePicker.showDateTimePicker(context,
                            showTitleActions: true,
                            minTime: DateTime.now(),
                            onConfirm: (date){
                              setState(() {
                                //_dateTime = date;
                                storage.write(key: SECURE_NOTE_KEY, value: date.toIso8601String());
                                testa = storage.read(key: SECURE_NOTE_KEY).toString();

                              });
                            },
                            currentTime: DateTime.now(),
                            locale: LocaleType.de
                            );
                          },
                          child: Text(
                            (testa == null ? 'Wählen sie Ihren nächsten Termin' : testa),
                            style: TextStyle(
                              fontSize: data.size.height / 29,
                              color: Colors.grey[400],
                            ),
                          ),
                        ),

But it doesn't work....

Teepause
  • 51
  • 1
  • 2
  • I think you should use the await keyword in front of the storage.write and storage.read function calls, since they are asyncronous. BTW sorry for the late reply. – Pedro R. Apr 01 '20 at 15:35