How can i store variable or to say list variable in the local storage in flutter?

1.2k views Asked by At

I am creating an music player app which needs to list down the music tile after user selected audio files from mobile local storage. So i need to store all the music files path in the local storage so when user selected music one time from local storage he/she did not need to select second time. I does not figure it out how can i do that. I use file_picker for selecting files and path_provider for storing the local data. Could you please help me with this situation please?

1

There are 1 answers

0
Manav On

You can store it in shared preferences , add the shared_preferences plugin to the pubspec.yaml file::

dependencies:
  flutter:
    sdk: flutter
  shared_preferences: "<newest version>"

Save data like :

// obtain shared preferences
final prefs = await SharedPreferences.getInstance();

// set value
prefs.setInt('counter', counter);

And finally read data using :

final prefs = await SharedPreferences.getInstance();

final counter = prefs.getInt('counter') ?? 0;

Note : Only primitive types can be used: int, double, bool, string, and stringList. It’s not designed to store a lot of data.