I have been searching on Stack Overflow for a long time, but I haven't found a suitable solution. Most of the answers either don't work or don't provide a clear answer, such as this post: SharedPreference giving "Unable to establish connection on channel." when used inside flutter_background_service. or https://stackoverflow.com/q/72880037/4964666
I am using a Flutter module in an iOS Swift app, and I am encountering the following error when initializing shared_preferences:
Unhandled Exception: PlatformException(channel-error, Unable to establish connection on channel., null, null)
This is my flutter doctor summary:
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.7.8, on macOS 12.6 21G115 darwin-x64, locale
zh-Hant-TW)
[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 14.1)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2022.1)
[✓] VS Code (version 1.76.1)
[✓] Connected device (5 available)
[✓] HTTP Host Availability
• No issues found!
Here is how I am referencing the Flutter module in my Swift code:
In AppDelegate.swift:
lazy var flutterEngine = FlutterEngine(name: "myModuleEngine")
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
flutterEngine.run()
GeneratedPluginRegistrant.register(with: self.flutterEngine)
return true
}
In ViewController.swift:
let flutterEngine = (UIApplication.shared.delegate as! AppDelegate).flutterEngine
var api: NativeDataSaveToFlutterAPI!
override func viewDidLoad() {
super.viewDidLoad()
api = NativeDataSaveToFlutterAPI(binaryMessenger: flutterEngine.binaryMessenger)
}
@IBAction func goToFlutterPage(_ sender: Any) {
self.flutterEngine.run(withEntrypoint: nil, initialRoute: "/")
api = NativeDataSaveToFlutterAPI(binaryMessenger: flutterEngine.binaryMessenger)
api.syncDataToFlutter(user: user) {
let flutterViewController = FlutterViewController(project: nil, initialRoute: "/", nibName: nil, bundle: nil)
self.present(flutterViewController, animated: true, completion: nil)
}
}
Flutter main.dart
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// The following code prints "before Shared init" but throws the error
// "Unhandled Exception: PlatformException(channel-error, Unable to establish connection on channel., null, null)"
// before it can print "after Shared init". However, the FlutterViewController is still being opened.
print('before Shared init');
await Shared.init();
print('after Shared init');
runApp(const MyExampleApp());
}
Flutter Shared class
class Shared {
static late SharedPreferences _prefs;
static init() async {
_prefs = await SharedPreferences.getInstance();
}
static void setInt(String key, int value) async {
await _prefs.setInt(key, value);
}
static int getInt(String key) {
return _prefs.getInt(key) ?? 0;
}
}
How can I resolve this error? I have confirmed that my Flutter module is functioning properly. Thank you!