Search code examples
androiddatetimeflutterdartstore

How to store the data from the DatePicker in Flutter


how do i store a the date which is set by the user such that every time, he/she opens the application, the date that is selected remains there and isn't refreshed to the initial date every time. Furthermore, how do I transfer the data that I have from say a supposed Screen1.dart to a Screen2.dart

Future<Null> _selectDate(BuildContext context) async {
final DateTime picked = await showDatePicker(
    context: context,
    initialDate: selectedDate,
    firstDate: DateTime(2015, 8),
    lastDate: DateTime(2101));
if (picked != null && picked != selectedDate)
  setState(() {
    selectedDate = picked;
  });
}

The data being the selectedDate. And the transfer refers to using this given data elsewhere to say find the difference between two chosen dates.


Solution

  • You could use shared preferences to achieve that, here is an example with a similar case as yours:

    How to save dateformat with sharedpref in flutter?

    saving: prefs.setString('dateTimeLastClicked', currentTime);

    loading: reftime_string = prefs.getString('dateTimeLastClicked');