Reputation: 509
I have been using Pigeon to call Android native side for data. However when i try to do the same thing in a WorkManager task, it just throws PlatformException(channel-error, Unable to establish connection on channel., null, null)
.
I suspect the channel is not connectable if it is on a different thread managed by WorkManager, but I have no idea how to fix it.
Pigeon files
@HostApi()
abstract class BridgeApiNative {
@TaskQueue(type: TaskQueueType.serialBackgroundThread)
String helloNative(String world);
}
Flutter code
@pragma('vm:entry-point')
void callbackDispatcher() {
Workmanager().executeTask((taskName, inputData) async {
switch (taskName) {
case "fetchBackground":
BridgeApiFlutter.setup(BridgeApiFlutterImpl());
BridgeApiNative().helloNative("hello!").then((value) => print("yeah $value"));
// FIXME: PlatformException(channel-error, Unable to establish connection on channel., null, null)
break;
}
return Future.value(true);
});
}
Future<void> main() async {
var instance = WidgetsFlutterBinding.ensureInitialized();
await Workmanager().initialize(callbackDispatcher, isInDebugMode: true);
await Workmanager().registerOneOffTask("1", "fetchBackground");
runApp(const MyApp());
}
Android side
class MainActivity : FlutterActivity() {
private lateinit var bridgeApiFlutter: BridgeApiFlutter
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
bridgeApiFlutter = BridgeApiFlutter(flutterEngine.dartExecutor.binaryMessenger)
}
override fun onResume() {
super.onResume()
Log.e("MainActivity", "onResume")
bridgeApiFlutter.helloFlutter("hello") {
Log.e("MainActivity", "result: $it")
// REMARK: This one works
}
}
}
Please help! Much appreciated!
Upvotes: 1
Views: 440