The update of Flutter to 3.27 is why it is good idea to have wrappers even for the components provided and developed by the Platform providers – in my case failing shared_preferences – especially when the provider is Google.
Failing with issues
There are several variations of issues:
- No implementation found for method getAll on channel plugins.flutter.io/shared_preferences https://stackoverflow.com/questions/74093954/how-to-fix-no-implementation-found-for-method-getall-on-channel-plugins-flutter
- SharedPreferencesAsyncPlatform.instance not set https://stackoverflow.com/questions/12604414/android-shared-preferences-set-in-an-asynctask-onpostexecute-not-always-set
- Something with Windows/Linux Shared Preferences not found and needing an update from the Package creator. It seems it may be something from the Flutter Engine itself https://github.com/flutter/flutter/issues/89211
General Issues
I’ve also faced several issues not related to shared_preferences The Major Problem is – they are not in my code, but in code of external libraries and packages.
- removal of deprecated properties and methods. For example the Color class has slashed out fields.
- different packages – depending on different versions of other libraries
Workarounds and Solutions
Option 0 is – upgrade only as very last measture or requirement. You rarely actually need the latest and greatest developments. At the end – you and all your users want stable and working software.
Option 1 – Downgrade
Flutter allows you to downgrade your version of the SDK. If not in your main place – you could download archived versions https://docs.flutter.dev/release/archive.
There is issue in this that – some of the packages may have upgraded to the latest version. Then, the newer APIs in dart and flutter will not be present – and you will need to downgrade the versions of packages too.
Option 2 – Replace
The the case of shared preferences – at the end you want simple Map of key-value storage. On the web there is such API – localStorage you could access it from dart/flutter code like this:
import 'dart:html' as html; html.window.localStorage[key] = 'value';// final value = html.window.localStorage[key] ?? ''
On the Desktop and Mobile Platforms you have
import 'dart:io'; import 'package:path_provider/path_provider.dart'; final file = File("${(await getApplicationDocumentsDirectory()).path}/$fileName"); if (await file.exists()) { var asStringSync = file.readAsStringSync(); if (asStringSync.isEmpty) { return "{}"; } return asStringSync;
And after returning a String from such method you could call simple
import 'dart:convert'; final map = jsonDecode(fileContent)
Unfortunately – this will abondon all values saved by your users so far. This may be valid case when
- launching a new app
- no critical data is saved in shared preferences
Check out more Flutter topics here: https://programtom.com/dev/category/software-development/flutter/