Saving a timer string to shared prefs

226

You're question is a bit unclear, so I am assuming you want to save the List<String> savedTime; into SharePreference and reload it again somehow.

saveTimeList(String key, List<String> savedTime) async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  prefs.setStringList(key, savedTime);
}

Retrieve like this:

Future<Set<String>> loadSavedTime(String key) async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  List<String> stringValue = prefs.getStringList(key) ?? [];
  #Input your additional code to convert String back to Time if you want
  #
  return stringValue.toSet();
}
Share:
226
Hunter Collingwood
Author by

Hunter Collingwood

Updated on December 23, 2022

Comments

  • Hunter Collingwood
    Hunter Collingwood over 1 year

    I am currently making a timer app that starts on tap and stops and lists the time on another tap in a sliding up panel. I have a class that gets the string of the timer which looks like this

    class Dependencies {
      final Stopwatch stopwatch = new Stopwatch();
      CurrentTime currentTime;
      List<String> savedTime = List<String>();
    
      transformTimeToString(int milliseconds) {
        currentTime = transformTimeToTime(stopwatch.elapsedMilliseconds);
    
        int hundreds = (milliseconds / 10).truncate();
        int seconds = (hundreds / 100).truncate();
        int minutes = (seconds / 60).truncate();
    
        String hundredsStr = (hundreds % 100).toString().padLeft(2, '0');
        String secondsStr = (seconds % 60).toString();
        String secondsMinStr = (seconds % 60).toString().padLeft(2, '0');
        String minutesStr = (minutes % 60).toString();
        if (currentTime.minutes > 0) {
          return '$minutesStr:$secondsMinStr.$hundredsStr';
        } else {
          return '$secondsStr.$hundredsStr';
        }
      }
    
      transformTimeToTime(int milliseconds) {
        int hundreds = (milliseconds / 10).truncate();
        int seconds = (hundreds / 100).truncate();
        int minutes = (seconds / 60).truncate();
    
        return CurrentTime(
          hundreds: hundreds % 100,
          seconds: seconds % 60,
          minutes: minutes % 60,
        );
      }
    }
    

    So I am having trouble listing that string on a json file to then save it to shared preferences. It would very helpful if someone with a great understanding could push me in the right direction with the code. Thanks Hunter

    Here's Links to everything Timer Page,Timer String, Whole project

    • ByteMe
      ByteMe over 3 years
      Add the code of what you've tried for saving the saving the string to SharedPreferences
  • thien nguyen
    thien nguyen over 3 years
    anywhere you like