Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 617
camilleB Avatar asked Jun 04 '18 19:06

camilleB


People also ask

What is SharedPreferences in flutter?

SharedPreferences. shared_preferences is a Flutter plugin that allows you to save data in a key-value format so you can easily retrieve it later. Behind the scenes, it uses the aptly named SharedPreferences on Android and the similar UserDefaults on iOS.

How to solve unhandled exception in flutter?

To solve Unhandled exception: MissingPluginException (No implementation found for method getAll on channel plugins.flutter.io/shared_preferences) error You have to Just add shrinkResources false and also minifyEnabled false in build Gradle OR You have to Uninstall your App First then Just Re-install your application.

How to fix flutter plugin dependency injection issue?

Answer : This issue is caused due to when you just add plugin and then get dependencies and just hot reload OR hot restart your flutter app. Hot restart or hot reload is not enough to trigger injection of plugin dependencies. You just have to close Or stop your app and then execute flutter run. That’s it. This will solve your issue.

Why is the Share plugin not working in flutter?

The share plugin works as intended. The error probably appears because the addition of the plugin was followed by hot restart or hot reload rather than full restart. This means the plugin's platform-specific code is not built into the app. Work around: stop the app and restart it after adding a plugin. Documentation PR: flutter/website#1038

Why am I getting missingpluginexception when registering plugins?

The MissingPluginException issues are raised due to incorrect registration of plugins.


2 Answers

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.

like image 91
kriss Avatar answered Oct 05 '22 19:10

kriss


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

like image 28
Siddharth Agrawal Avatar answered Oct 05 '22 19:10

Siddharth Agrawal