handleMethodCall method
Handles method calls over the MethodChannel of this plugin. Note: Check the "federated" architecture for a new way of doing this: https://flutter.dev/go/federated-plugins
Implementation
Future<dynamic> handleMethodCall(MethodCall call) async {
if (call.method == 'init') {
var args = call.arguments;
String apiKey = args['apiKey'];
JSObject configuration = getConfiguration(call);
// Set library
Amplitude instance = amplitude.createInstance();
instance.add(createJSInteropWrapper(FlutterLibraryPlugin(
args['library'] ?? 'amplitude_flutter/unknown')));
instance.init(apiKey, configuration);
instances[args['instanceName'] ?? Constants.defaultInstanceName] =
instance;
return null;
}
Amplitude? instance = instances[
call.arguments['instanceName'] ?? Constants.defaultInstanceName];
if (instance == null) {
throw Exception(
'instance not found: ${call.arguments['instanceName'] ?? Constants.defaultInstanceName}');
}
switch (call.method) {
case "track":
case "identify":
case "groupIdentify":
case "setGroup":
case "revenue":
{
JSObject event = getEvent(call);
instance.track(event);
}
case "getUserId":
{
JSString? userId = instance.getUserId();
if (userId == null) {
return null;
}
return userId.toDart;
}
case "setUserId":
{
Map args = call.arguments['properties'];
String? userId = args['setUserId'];
instance.setUserId(userId?.toJS);
}
case "getDeviceId":
{
return instance.getDeviceId()?.toDart;
}
case "setDeviceId":
{
Map args = call.arguments['properties'];
String? deviceId = args['setDeviceId'];
instance.setDeviceId(deviceId?.toJS);
}
case "getSessionId":
{
return instance.getSessionId()?.toDartInt;
}
case "reset":
{
instance.reset();
}
case "flush":
{
instance.flush();
}
case "setOptOut":
{
Map<String, dynamic> args = call.arguments['properties'];
bool enabled = args['setOptOut'];
instance.setOptOut(enabled);
}
default:
throw PlatformException(
code: 'Unimplemented',
details:
"The amplitude_flutter plugin for web doesn't implement the method '${call.method}'",
);
}
}