Flutter SharedPreference do not persist

后端 未结 3 1265
长情又很酷
长情又很酷 2021-01-18 11:55

I want to save user preferences using Flutter\'s SharedPreference. But the registered preferences are ALL null at new start (when app have been closed, not unis

3条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-18 12:43

    I had the same issue and fixed it in Android/../MainActivity.java by adding at the top:

    import io.flutter.plugins.GeneratedPluginRegistrant;
    

    As well as under super.onCreate(savedInstanceState);

    GeneratedPluginRegistrant.registerWith(this);
    

    The problem comes from using

    SharedPreferences.setMockInitialValues({}); 
    

    I got it from Flutter Test MissingPluginException but it seems to clear all the shared preferences.

    However if you remove SharedPreferences.setMockInitialValues({}); and you don't have the two lines above in MainActivity.java, you'll get:

    MissingPluginException(No implementation found for method getAll on channel flutter: plugins.flutter.io/shared_preferences)

    I hope it helps!

提交回复
热议问题