Skip to content

Instantly share code, notes, and snippets.

@axemclion
Created December 2, 2018 21:17
Show Gist options
  • Save axemclion/0413850124d3fd2939dcb285dabbadc9 to your computer and use it in GitHub Desktop.
Save axemclion/0413850124d3fd2939dcb285dabbadc9 to your computer and use it in GitHub Desktop.
An adapter to use React Native's Native Modules in Flutter
package com.example.helloflutterplugin;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.modules.toast.ToastModule;
import com.nparashuram.reactnative.ReactNativeModuleAdapter;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;
import java.lang.reflect.InvocationTargetException;
/** HelloFlutterPlugin */
public class FlutterToastPlugin implements MethodCallHandler {
private static ReactNativeModuleAdapter mReactNativeModule;
/** Plugin registration. */
public static void registerWith(Registrar registrar) {
final MethodChannel channel = new MethodChannel(registrar.messenger(), "flutter_toast_plugin");
channel.setMethodCallHandler(new FlutterToastPlugin());
mReactNativeModule =
new ReactNativeModuleAdapter(
new ToastModule(new ReactApplicationContext(registrar.context())));
}
@Override
public void onMethodCall(MethodCall call, Result result) {
try {
mReactNativeModule.invokeMethod(call.method, call.arguments);
} catch (IllegalAccessException e) {
result.notImplemented();
} catch (InvocationTargetException e) {
result.notImplemented();
}
}
}
package com.nparashuram.reactnative;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactMethod;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class ReactNativeModuleAdapter {
private final NativeModule mNativeModule;
private Map<String, Method> mMethods;
public ReactNativeModuleAdapter(NativeModule nativeModule) {
mNativeModule = nativeModule;
}
public void invokeMethod(String name, Object arguments) throws IllegalAccessException, InvocationTargetException {
if (mMethods == null) {
mMethods = initializeMethods(mNativeModule);
}
if (mMethods.containsKey(name)) {
mMethods.get(name).invoke(mNativeModule, ((ArrayList) arguments).toArray());
} else {
// If unimplemented, throw an exception
throw new IllegalAccessException("Method not implemented " + name);
}
}
private static Map<String, Method> initializeMethods(NativeModule nativeModule) {
Class<? extends NativeModule> classForMethods = nativeModule.getClass();
Method[] targetMethods = classForMethods.getDeclaredMethods();
final Map<String, Method> methods = new HashMap<>();
for (Method targetMethod : targetMethods) {
ReactMethod annotation = targetMethod.getAnnotation(ReactMethod.class);
if (annotation != null) {
String methodName = targetMethod.getName();
if (methods.containsKey(methodName)) {
// Method overloading not supported as js sees it as an object regardless of arg count
throw new IllegalArgumentException("Method name already registered: " + methodName);
}
methods.put(methodName, targetMethod);
}
}
return methods;
}
}
@DarthSid12
Copy link

Hey, how can I use this? Please let me know

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment