'SharedPreference giving "Unable to establish connection on channel." when used inside flutter_background_service
I'm using flutter_background_service to start a service to fetch data from Cloud. It's working just fine in Android. But when I run on an iOS Simulator, I get the Above mentioned error.
void syncData() async {
WidgetsFlutterBinding.ensureInitialized();
Fimber.plantTree(DebugTree());
Fimber.d("SyncData() called");
final service = FlutterBackgroundService();
Fimber.d("Sync Service Started: $service");
ApiRepo apiRepo = ApiRepo();
bool isPatientUploaded = false;
Fimber.d("isPatientUploaded: $isPatientUploaded");
if(Platform.isAndroid) {
SharedPreferencesAndroid.registerWith();
PathProviderAndroid.registerWith();
FlutterBackgroundServiceAndroid.registerWith();
} else if (Platform.isIOS) {
SharedPreferencesIOS.registerWith();
PathProviderIOS.registerWith();
FlutterBackgroundServiceIOS.registerWith();
}
Fimber.d("Fetching Shared Preference");
SharedPreferences prefs = await SharedPreferences.getInstance();
Fimber.d("Fetched Shared Preference: $prefs");
String? authToken = prefs.getString(PrefKeys.TOKEN);
...........
}
I can see the Log till "Fetching Shared Preference" and then get an error afterwards. I don't see the Next log.
[VERBOSE-2:ui_dart_state.cc(209)] Unhandled Exception: PlatformException(channel-error, Unable to establish connection on channel., null, null)
This is how I'm starting the Service
startSync() async {
Fimber.d("startSync() called");
final service = FlutterBackgroundService();
Fimber.i("Configuring $service");
bool? isConfigured = await service.configure(
iosConfiguration: IosConfiguration(
onForeground: syncData, onBackground: syncData, autoStart: false),
androidConfiguration: AndroidConfiguration(
onStart: syncData,
isForegroundMode: true,
autoStart: false,
foregroundServiceNotificationContent: "Uploading Data to Cloud...",
foregroundServiceNotificationTitle: "Selfi",
));
await DatabaseHelper().db;
Fimber.i("isConfigured: $isConfigured");
if (isConfigured) {
Fimber.d("Starting Service");
bool isStarted = await service.startService();
Fimber.d("Service Started: $isStarted");
}
}
Solution 1:[1]
You can stop and restart the app.. Sometimes the IDE doesn't pick the package installed instantly. hope it helps :)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Riyazat Durrani |