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

IosSharedpreferencesFlutter

Ios Problem Overview


My Flutter application uses the Flutter SharedPreferences plugin and send values to the iOS side with platform.invokeMethod. If I start the application, I have this error:

[VERBOSE-2:dart_error.cc(16)] Unhandled exception:
MissingPluginException(No implementation found for method getAll on channel plugins.flutter.io/shared_preferences)
#0      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:278:7)
<asynchronous suspension>
#1      SharedPreferences.getInstance (package:shared_preferences/shared_preferences.dart:25:27)
<asynchronous suspension>
#2      main (file:///Users/Developer/workspace/flutter-app/q_flutter2/lib/main.dart:25:53)
<asynchronous suspension>
#3      _startIsolate.<anonymous closure> (dart:isolate/runtime/libisolate_patch.dart:279:19)
#4      _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:165:12)

If I comment the function to send the value to iOS side, the error is not displayed and the SharedPreferences is working.

Someone can help me?

Ios Solutions


Solution 1 - Ios

> No implementation found for method getAll on channel plugins.flutter.io.

The above will occur when you setup the plugin for the first time, so it has to be reinstalled.

Therefore, uninstall and reinstall your application.

Solution 2 - Ios

After doing a lot of research, I found the answer. Add this to your code before you use shared preferences.

SharedPreferences.setMockInitialValues({});

It is because if you use getAll where there is nothing, it will go crazy. I don't think it is anything to do with iOS. If you even use normal getString, the internal program uses getAll so it will still crash

EDIT

I have been receiving a lot of comments on how it does not persist data b/w sessions. What you can do to fix that is put it in a try and catch statement. In try, call sharedPreferences.get and catch the error and then do setMockInitialValues. If sharedPreferences.get does not give an error, it means there is already data and no need to set mock values replacing the old ones.

I am not sure if that will work so if anyone tries it and it helps in persisting data, let me know so that I can validate it for others

EDIT 2

Thanks to Edwin Nyawoli, I now know why it did not persist data in between sessions. While mine is a temporary and not a real solution, it still may help. If someone can help me recreate this issue on my new device, I can try to figure out a new solution. Please let me know if you are willing to upload your project to github so that I can recreate it. For now, I did some more research and believe this might help you:-

In /android/app/build.gradle

buildTypes {
    release {
        signingConfig signingConfigs.release

        minifyEnabled true
        useProguard true

        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

change to

buildTypes {
    release {
        signingConfig signingConfigs.release

        minifyEnabled true
        useProguard true

        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}

This answer is not mine, its from this comment on a github issue https://github.com/flutter/flutter/issues/65334#issuecomment-760270219

Solution 3 - Ios

SOLUTION 

Step 1: Update buildTypes in /android/app/build.gradle


android {
    compileSdkVersion 30

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    lintOptions {
        disable 'InvalidPackage'
    }

    defaultConfig {
       ...
    }

    signingConfigs {
        release {
            ...
        }
    }

    buildTypes {
        debug {
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }

        release {
            // TODO: Add your own signing config for the release build.
            // Signing with the debug keys for now, so `flutter run --release` works.
            // signingConfig signingConfigs.debug

            signingConfig signingConfigs.release

            minifyEnabled false
            shrinkResources false

            useProguard true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

Step 2: Add file android/app/proguard-rules.pro:

-keep class androidx.lifecycle.DefaultLifecycleObserver

Step 3: Update MainActivity


import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugins.GeneratedPluginRegistrant

class MainActivity: FlutterActivity() {
  override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
    GeneratedPluginRegistrant.registerWith(flutterEngine) // add this line
  }
}

Solution 4 - Ios

I had this issue before and this is what I have done to fix it:

  1. Run flutter clean
  2. Run flutter pub get

after that delete your debug app from the device.

Solution 5 - Ios

This gave me a nightmare as well.

  • minifyEnabled false
  • shrinkResources false

    Adding these two lines inside buildTypes in app/build.gradle worked for me.
android {
    //

    defaultConfig {
        ...
    }

    buildTypes {
        release {
            signingConfig signingConfigs.debug
            minifyEnabled false
            shrinkResources false
        }
    }
}

I've worked with shared_prefs before but got stuck in this one on my recent project. The community shall look into this.

Solution 6 - Ios

On 14.02.2022 i had this problem with shared_preferences^2.0.8 when used it in background service. Adding these lines in pubspec.yaml fixed the problem:

dependency_overrides:
  shared_preferences_android: 2.0.10 

Solution 7 - Ios

Got the same problem after adding Shared Preferences to my project and running it on an emulator. The following steps solved the problem for me:

  1. Run 'flutter clean'
  2. Run 'flutter pub get'
  3. Delete the app from the emulator

Solution 8 - Ios

For android part, you should not use

SharedPreferences.setMockInitialValues({});

because it's not persisting your preference.

instead, add the following line to your app/proguard-rules.pro:

-keep class androidx.lifecycle.DefaultLifecycleObserver

see https://github.com/flutter/flutter/issues/58479#issuecomment-734099445

See related issue: https://github.com/flutter/flutter/issues/65334

Solution 9 - Ios

So after creating flutter a dummy project to test the solution here to the 'T', which didn't work for me. I finally resolved it by updating the MainActivity.kt file.

My solution was simply to add the line flutterEngine.getPlugins().add(SharedPreferencesPlugin())

Here is the Full MainActivity.kt file :

package com.example.begin.init_begin
import android.content.Context
import android.os.Bundle
import androidx.annotation.NonNull
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugins.sharedpreferences.SharedPreferencesPlugin

class MainActivity: FlutterActivity() {

override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
    super.configureFlutterEngine(flutterEngine)
    flutterEngine.getPlugins().add(SharedPreferencesPlugin())

}

}

Please note: The GeneratePluginRegistrant.java already has the registration but in my case adding it as shown above is what resolves the issue.

Solution 10 - Ios

Faced this issue and fixed by completely reinstalling the app

Solution 11 - Ios

In my case, I removed the package "flutter_facebook_login: ^3.0.0" in pubspec.yaml file, and then it is working well...

I have tried all the above solutions, but there is not any other better solution than removing the "flutter_facebook_login" package.

In some above solutions MissingPluginException could be fixed, but get() and set() of shared_preferences are not working properly.

Please let me know about a better solution.

Solution 12 - Ios

Check AppDelegate. Channel registration should be after plugins registration

...
@objc class AppDelegate: FlutterAppDelegate {

    override func application(
    ...
        GeneratedPluginRegistrant.register(with: self)
        channel = FlutterMethodChannel.init(name: "dressme.lofesdev.com/geo",
                                        binaryMessenger: controller);
    ...

Solution 13 - Ios

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

then add this to your in main() function of main.dart file

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

this worked for me

Solution 14 - Ios

I have tried all the above-suggested solutions when working with Workmanager for background fetch, nothing solved the problem. :(

Finally, I did as below, the issue is resolved.

In main.dart:

if (Platform.isAndroid) {
        SharedPreferencesAndroid.registerWith();
      } else if (Platform.isIOS) {
        SharedPreferencesIOS.registerWith();
      }

In pubspec.yaml:

shared_preferences_ios:
shared_preferences_android:

Solution 15 - Ios

Maybe this will help if the above answers won't work. While creating the android studio flutter project i forgot to check the boxes for including kotlin support for android / swift support for iOS. It was so because after flutter clean and flutter run my project didn't work.

Solution 16 - Ios

I also had this error using in Debugging mode using VS Code. I just stop, then start again the app (without to directly restart).

Run > Stop Debugging

Run > Start Debugging

Solution 17 - Ios

In my solution, I haven't used SharedPreferences.setMockInitialValues({}); as other folks fairly said that it shall be used for test (and IDE also says so), but not for production app. So I noticed that Flutter says that my project uses old flutter android embedded version and I should upgrade to v2. And I did so, using this instruction: https://github.com/flutter/flutter/wiki/Upgrading-pre-1.12-Android-projects

After that the error about MissingPluginException disappeared. So it seems a bit better solution, than calling method for test. I wish I could know why, but at this moment I just don't have time to dig too much inside. Anyway, this is another solution to this issue.

Solution 18 - Ios

Just use

classpath 'com.android.tools.build:gradle:3.5.1'

inside your android/build.gradle

Solution 19 - Ios

I've encountered the same issue while using background_fetch when the app is terminated and the headless task was executed with a callback that wanted to access shared_preferences package.

import 'package:shared_preferences_ios/shared_preferences_ios.dart';
import 'package:shared_preferences_android/shared_preferences_android.dart';
import 'dart:io';

///Other code

//Add the check and registerWtih in your desired method.
if (Platform.isAndroid) {
  SharedPreferencesAndroid.registerWith();
} else if (Platform.isIOS) {
  SharedPreferencesIOS.registerWith();
}

Code using background_fetch:

import 'package:shared_preferences_android/shared_preferences_android.dart';
import 'dart:io';

///Other code

void backgroundFetchHeadlessTask(HeadlessTask task) async {
  SharedPreferencesAndroid.registerWith();
  //No need to check OS because headless task works only for Android

///code code
}

Solution 20 - Ios

In my case, reinstalling the application didn't work.

Instead, go to the AppDelegate.m file, path ios/runner/AppDelegate.m:

#include "AppDelegate.h"
#include "GeneratedPluginRegistrant.h"

@import Firebase;

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [FIRApp configure];
    [GeneratedPluginRegistrant registerWithRegistry:self];
    return [super application:application didFinishLaunchingWithOptions:launchOptions];
}


@end

Then delete your application from the device then run the application again.

Solution 21 - Ios

Run flutter clean and after that Run flutter pub get

Usually if you implemented the shared preferences package during your debugging, it may causes this issue, so try to stop the debug the method above and it should work.

Solution 22 - Ios

In my case, I solved this by adding bellow line in /android/app/build.gradle

buildTypes {
    release {
        signingConfig signingConfigs.release
    }
}

To

buildTypes {
        release {
            signingConfig signingConfigs.release
            
            minifyEnabled true
            useProguard true

            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
}

Solution 23 - Ios

I was receiving this issue with one of the flutter packages and upgrading the package to the latest version helped.

Solution 24 - Ios

I got this issue in Flutter Web. I added the code, used "fast restart" and got this error. Stopping and restarting solved the problem for me.

Solution 25 - Ios

I had the same issue a couple of days ago, after days of googling I found out that the reason shared preferences is not working, is that I had a package that had not been properly configured "flutter_facebook_login". Thus any package after Facebook, will not work, in this case, "shared preferences".

Solution: Make sure every package you are using has been fully configured or remove the package that is causing the problem.

Solution 26 - Ios

Tried everything I could find, none of it worked. Was reluctant when I saw comment with -2 suggesting flutter_fackbook_login package was causing the issue. Later again found here about flutter_fackbook_login being the issue. After removing the package it worked!

Solution 27 - Ios

When i tried to load data in excel file I also faced this same problem. tried all stuff as suggested above. But at when i installed Microsoft excel in my emulator it solved instantly.

Solution 28 - Ios

This main.dart add line

SharedPreferences.setMockInitialValues({});

I am solved

Solution 29 - Ios

In my case, it was just increasing the minSdkVersion. I found this when I started my app from cold (not by clicking the restart button). -

The plugin flutter_webrtc requires a higher Android SDK version.
Fix this issue by adding the following to the file C:\Users\Piyush\Documents\filesfi\android\app\build.gradle:
android {
  defaultConfig {
    minSdkVersion 21
  }
}

Solution 30 - Ios

As in my case, I have a flutter MODULE embedded in an iOS application. The MissingPluginException issues are raised due to incorrect registration of plugins.

The following code example works:

import Flutter
import FlutterPluginRegistrant

@main 
class AppDelegate: FlutterAppDelegate {

    ...

    var flutterEngine: FlutterEngine!

    ...

    func application(
        _ application: UIApplication, 
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool 
    {
        // Override point for customization after application launch.
        self.flutterEngine = FlutterEngine(name: "my flutter engine")
        self.flutterEngine.run()
        // register plugins onto engine, NOT AppDelegate itself;
        GeneratedPluginRegistrant.register(with: self.flutterEngine)
        return true
    }

    ...

}

Solution 31 - Ios

AndroidManifest.xml

    <activity
        android:name="io.flutter.embedding.android.FlutterActivity">
    </activity>
    <meta-data
        android:name="flutterEmbedding"
        android:value="2" />

Mark these 2 code properly as I was changing the flutter embedding to v2 it occurred so I properly checked these 2 code and it removed my error.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncamilleBView Question on Stackoverflow
Solution 1 - IoskrissView Answer on Stackoverflow
Solution 2 - IosSiddharth AgrawalView Answer on Stackoverflow
Solution 3 - IosPetrus Nguyễn Thái HọcView Answer on Stackoverflow
Solution 4 - IosAmjad AlwarehView Answer on Stackoverflow
Solution 5 - IosYuvraj SinghView Answer on Stackoverflow
Solution 6 - IosViktar KView Answer on Stackoverflow
Solution 7 - IospositrixView Answer on Stackoverflow
Solution 8 - Iosישו אוהב אותךView Answer on Stackoverflow
Solution 9 - IosDainShardzView Answer on Stackoverflow
Solution 10 - IosikhsanudinhakimView Answer on Stackoverflow
Solution 11 - Iospersec10000View Answer on Stackoverflow
Solution 12 - IosDmitryView Answer on Stackoverflow
Solution 13 - IosNeils' OutletView Answer on Stackoverflow
Solution 14 - IosAchillesView Answer on Stackoverflow
Solution 15 - IosOmiView Answer on Stackoverflow
Solution 16 - IosAdrien ArcuriView Answer on Stackoverflow
Solution 17 - IosKonstantin KozirevView Answer on Stackoverflow
Solution 18 - IosM.AQIBView Answer on Stackoverflow
Solution 19 - IosPhilipos D.View Answer on Stackoverflow
Solution 20 - IosAbel TilahunView Answer on Stackoverflow
Solution 21 - IosMr. WreckerView Answer on Stackoverflow
Solution 22 - IosShahariar KabirView Answer on Stackoverflow
Solution 23 - IosramView Answer on Stackoverflow
Solution 24 - IosAzureIPView Answer on Stackoverflow
Solution 25 - IosLuis AltView Answer on Stackoverflow
Solution 26 - IoszorroView Answer on Stackoverflow
Solution 27 - IosA.K.M. Arifur RahmanView Answer on Stackoverflow
Solution 28 - IosMehmet Emin SayımView Answer on Stackoverflow
Solution 29 - IosPiyushView Answer on Stackoverflow
Solution 30 - IosKe YangView Answer on Stackoverflow
Solution 31 - IosSpsnamtaView Answer on Stackoverflow