SharedPreferences not instantiating in release build
Asked Answered
C

8

14

In my flutter application I have implemented a an onboarding view. As it should load only once, I have used shared preferences to store an integer to indicate that onboarding is already shown. When I run the application in debug mode everything works perfectly. But when I build the release version of it, it doesn’t work.

And also my application uses firebase mobile authentication. I am mentioning this since it may be a reason as well.

The code:

case InitializeEvent:
        SharedPreferences prefs = await SharedPreferences.getInstance();
        int initScreen = prefs.getInt(SharedPrefUtil.INIT_SCREEN);
        await prefs.setInt(SharedPrefUtil.INIT_SCREEN, 1);
        if (initScreen == 1) {
          add(CheckAppConfigEvent());
        } else {
          yield state.clone(page: RootState.ONBOARDING_PAGE);
        }
        yield state.clone(loading: false);
        break;

So in the above code, if I comment initializing shared pref, reading and writing lines and set true or false in if else statement everything works fine in release build. That’s why I think the issue is in initializing shared preferences.

And also I have given permission only for internet. Am I missing any permission in AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET"/>
Crocus answered 22/7, 2020 at 11:59 Comment(5)
So I figured out the issue. If I build it using the command flutter build apk --no-shrink it works fine in the release build as well. So if there are any solutions ot apply code shrinkage that would be appreciable.Crocus
I have the same exact issue and can't find any solution too!Bello
I'm also having same problem and couldn't find solution. Let us know here if anyone found solution.Pneumoencephalogram
I still didn't find anything. It looks like a very severe yet rare bug in package:shared_preferences.Bello
I have also this issue: In Debug it works perfectly, in release - there is no error but nothing is being stored in the SharedPreferences... So next time I restart the app, everything is empty. --no-shrink doesn't help either.Carlos
G
10

My app was also not working in the release build and the issue is due to the shared_preferences package.

I'm getting the following error in the release build:

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

I tried several solutions:

  1. switching the flutter channel
  2. tried several versions of shared_preferences
  3. deleting the .pub-cache folder as described here https://github.com/flutter/flutter/issues/38873#issuecomment-680788165

but nothing worked.

The app worked fine in the debug build but not in the release build.

Then I tried

flutter build apk --no-shrink

flutter build appbundle --no-shrink

and the app worked fine in the release build as well. But the issue is resolved without shrinking the code. So this needs to be fixed.

Hopefully, the Flutter team will look into the issue and will resolve that.

Edit:
Already reported the issue to the Flutter team.
https://github.com/flutter/flutter/issues/65334

Garica answered 6/9, 2020 at 11:29 Comment(4)
It seems that you've done some research. Would you create an Issue on shared_preferences' GitHub repo? I have the same problem.Bello
@BartekPacia done. github.com/flutter/flutter/issues/65334Garica
Thank you so much bro. i wish you were here to know how happy i am . you saved my life , i have been working for two days on this sh*t . Thank you so much .Dannielledannon
Hey, @DeepakGoyal Maybe a bit late, but I struggle with this problem as well. But flutter build apk --no-shrink made no difference for me. Do you have another solution?Crossfade
G
11

The previous solutions did not work in my case, then I tried disabling these two then it worked.

minifyEnabled false shrinkResources false

Geddes answered 21/3, 2021 at 16:8 Comment(1)
Mine wasn't a flutter project and so the other solutions didn't work for me. Making these changes in my build.gradle file worked a charm. Many thanks!Porthole
G
10

My app was also not working in the release build and the issue is due to the shared_preferences package.

I'm getting the following error in the release build:

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

I tried several solutions:

  1. switching the flutter channel
  2. tried several versions of shared_preferences
  3. deleting the .pub-cache folder as described here https://github.com/flutter/flutter/issues/38873#issuecomment-680788165

but nothing worked.

The app worked fine in the debug build but not in the release build.

Then I tried

flutter build apk --no-shrink

flutter build appbundle --no-shrink

and the app worked fine in the release build as well. But the issue is resolved without shrinking the code. So this needs to be fixed.

Hopefully, the Flutter team will look into the issue and will resolve that.

Edit:
Already reported the issue to the Flutter team.
https://github.com/flutter/flutter/issues/65334

Garica answered 6/9, 2020 at 11:29 Comment(4)
It seems that you've done some research. Would you create an Issue on shared_preferences' GitHub repo? I have the same problem.Bello
@BartekPacia done. github.com/flutter/flutter/issues/65334Garica
Thank you so much bro. i wish you were here to know how happy i am . you saved my life , i have been working for two days on this sh*t . Thank you so much .Dannielledannon
Hey, @DeepakGoyal Maybe a bit late, but I struggle with this problem as well. But flutter build apk --no-shrink made no difference for me. Do you have another solution?Crossfade
T
4

I also faced the same problem. In my case the problem is not my code, the problem is the Gradle version. When I used any Gradle version greater than 4 then this problem occur. But when I reduced the Gradle version to 3.5.0 then It works fine. I also tested with Gradle 3.6.4 it also works fine.

Change this

dependencies {
        classpath 'com.android.tools.build:gradle:4.1.1'
    }

to

dependencies {
        classpath 'com.android.tools.build:gradle:3.6.4'
    }

Note: In flutter don't always upgrade your Gradle version

Thoughtless answered 4/1, 2021 at 4:49 Comment(0)
P
3

I just changed :

dependencies {
   classpath 'com.android.tools.build:gradle:**4.1.0**'
}

TO

dependencies {
   classpath 'com.android.tools.build:gradle:**3.5.0**'
}

Then

  • flutter clean
  • flutter build apk --release

it will work for sure.

Proponent answered 20/3, 2021 at 5:7 Comment(0)
A
2

Building with flutter build apk --no-shrink solved to me

Attenuant answered 3/9, 2020 at 12:28 Comment(1)
Well but I do want to take advantage of Dart's tree shaking.Bello
B
1

it's really works for me and you can try----

use -> SharedPreferences.setMockInitialValues({});

void main() {
  SharedPreferences.setMockInitialValues({});

  SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
      systemNavigationBarColor: Colors.indigo[900], // navigation bar color
      statusBarColor: Colors.indigo[900],
      systemNavigationBarIconBrightness: Brightness.light));
  runApp(MyApp());
}

enter image description here

Bergmann answered 24/2, 2021 at 6:59 Comment(0)
M
0

replacing classpath 'com.android.tools.build:gradle:4.1.0' with classpath 'com.android.tools.build:gradle:3.5.0' in android/build.gradle file fixed the problem for me

Menstruate answered 25/1, 2021 at 22:20 Comment(0)
S
0

Maybe u have forgetten to add this line of code on MainActivity.java `@Override

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GeneratedPluginRegistrant.registerWith(this.getFlutterEngine());
 }`

It worked on me

Surprisal answered 6/3, 2021 at 7:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.