Flutter: shared preferences

后端 未结 2 644
后悔当初
后悔当初 2021-02-08 10:23

I have this function:

  Future load(SharedPreferences prefs, String fileName) async {
    prefs = await SharedPreferences.getInstance();
    String         


        
2条回答
  •  自闭症患者
    2021-02-08 11:09

    Shared Preferences

    In Flutter, Shared Preferences are used to store primitive data (int, double, bool, string, and stringList). This data is associated with the app, so when the user uninstalls your app, the data will also be deleted.

    Get the plugin

    The shared_preferences plugin from pub is a wrapper around Android SharedPreferences and iOS NSUserDefaults. You can get this plugin by adding the shared_preferences line to your pubspec.yaml file in the dependencies section.

    dependencies:
      shared_preferences: '>=0.5.12+2 <2.0.0'
    

    You can change the version number to whatever the current one is, but anything less than 2.0 should be compatible.

    Import the package

    In whichever file you need the Shared Preferences, add the following import:

    import 'package:shared_preferences/shared_preferences.dart';
    

    Reading and writing data

    To get the shared preferences object you can do the following:

    final prefs = await SharedPreferences.getInstance();
    

    This will be used for all of the following examples.

    int

    • read: final myInt = prefs.getInt('my_int_key') ?? 0;
    • write: prefs.setInt('my_int_key', 42);

    double

    • read: final myDouble = prefs.getDouble('my_double_key') ?? 0.0;
    • write: prefs.setDouble('my_double_key', 3.14);

    bool

    • read: final myBool = prefs.getBool('my_bool_key') ?? false;
    • write: prefs.setBool('my_bool_key', true);

    string

    • read: final myString = prefs.getString('my_string_key') ?? '';
    • write: prefs.setString('my_string_key', 'hello');

    stringList

    • read: final myStringList = prefs.getStringList('my_string_list_key') ?? [];
    • write: prefs.setStringList('my_string_list_key', ['horse', 'cow', 'sheep']);

    Removing data

    You can remove any saved data by supplying the key name:

    prefs.remove('my_int_key');
    

    I rarely find a need to do that, though. I just overwrite the old data or ignore it. You shouldn't store any sensitive data in Shared Preferences.

    See also

    • Shared Preferences Service in Flutter for Code Maintainability
    • Documentation: Storing key-value data on disk
    • What are the ?? double question marks in Dart?
    • How to create an empty list in Dart

提交回复
热议问题