A handler plugin package that provides a cross-platform solution for handling platform message channel notifications.
- Create a custom class for the platform handler.
class Handler extends PlatformHandler {
@override
Future<dynamic> n2fCallDispatcher(MethodCall call) async {
print("call.method: ${call.method}, call.arguments: ${call.arguments}");
switch (call.method) {
case "LogCallback": //日志回调
String tag = call.arguments["tag"];
String message = call.arguments["message"];
print("$tag: $message");
break;
}
return super.n2fCallDispatcher(call);
}
}- Define a custom class for the platform notification.
class FusedLocationCallback extends PlatformNotification {
int lastUpdatedTimestamp = 0;
List<Completer> _completers = [];
Timer? updateLatestLocationTimer;
FusedLocationCallback() {
subscribers = {
"fineLocationCallback":
fusedLocationResultCallback, // fine location update
"coarseLocationCallback":
fusedLocationResultCallback, // coarse location update
};
}
@override
void dockingOn({Completer? completer}) {
if (completer != null && !completer.isCompleted) {
_completers.add(completer);
}
_completers = _completers.where((element) => !element.isCompleted).toList();
docking = true;
}
void fusedLocationResultCallback(dynamic callbackJson) {
Map<String, dynamic>? result = json.decode(callbackJson);
print("The fused location updated: $result");
_completers.map((e) => e.complete(0)).toList();
_completers = [];
}
}- Create an instance of the platform handler and set the method call handler.
import 'package:enhanced_change_notifier/enhanced_change_notifier.dart';
const MethodChannel _methodChannel = MethodChannel('native.demo.com/messageChannel');
final GlobalFactory<Handler> handler =
GlobalFactory(() => Handler());
_methodChannel.setMethodCallHandler(handler.getInstance().n2fCallDispatcher);
FusedLocationCallback fusedLocationCallback = FusedLocationCallback();
List<PlatformNotification> listeners = [fusedLocationCallback];
handler.getInstance().subscribe(listeners);
fusedLocationCallback.dockingOn();
_methodChannel.invokeMethod("locationUpdate");Feel free to file an issue if you have any problem.