How to add a Flutter Plugin without publishing to Pub (Android)
Feb 13, 2019(6 years ago)
I was looking for a way to quickly create Flutter Plugin. I can’t seem to find the exact same plugin on https://pub.dev or any solution about embedding the plugin directly into your Flutter App.
The plugin that I want to implement is the Facebook App Events plugin. Since I needed this plugin for the Android platform only. I wasn’t about to publish a half complete Plugin into https://pub.dev.
Step 1: Create the Dart files in /lib
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:meta/meta.dart';
class FacebookAppEvents {
factory FacebookAppEvents() => _instance;
FacebookAppEvents.private(MethodChannel platformChannel) : _channel = platformChannel;
final MethodChannel _channel;
static final FacebookAppEvents _instance = FacebookAppEvents.private(const MethodChannel('facebook_app_events'));
Future<void> logEvent({@required String name, Map<String, dynamic> parameters}) async {
await _channel.invokeMethod('logEvent', <String, dynamic>{
'name': name,
'parameters': parameters,
});
}
Future<void> setUserId(String id) async {
await _channel.invokeMethod('setUserId', id);
}
Future<void> clearUserData() async {
await _channel.invokeMethod('clearUserData');
}
}
Step 2: Create the Java Plugin in /android/app/src/
package app.munch.facebookappevents;
import android.os.Bundle;
import com.facebook.appevents.AppEventsLogger;
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.util.Map;
public class FacebookAppEventsPlugin implements MethodCallHandler {
private final AppEventsLogger logger;
public FacebookAppEventsPlugin(Registrar registrar) {
this.logger = AppEventsLogger.newLogger(registrar.activity());
}
public static void registerWith(Registrar registrar) {
final MethodChannel channel = new MethodChannel(registrar.messenger(), "facebook_app_events");
channel.setMethodCallHandler(new FacebookAppEventsPlugin(registrar));
}
@Override
public void onMethodCall(MethodCall call, Result result) {
switch (call.method) {
case "logEvent":
handleLogEvent(call, result);
break;
// ...
default:
result.notImplemented();
break;
}
}
private void handleLogEvent(MethodCall call, Result result) {
@SuppressWarnings("unchecked")
Map<String, Object> arguments = (Map<String, Object>) call.arguments;
final String eventName = (String) arguments.get("name");
@SuppressWarnings("unchecked") final Bundle parameterBundle =
createBundleFromMap((Map<String, Object>) arguments.get("parameters"));
logger.logEvent(eventName, parameterBundle);
result.success(null);
}
}
Step 3: Register in MainActivity.java
MainActivity.java
package app.munch.munchapp;
import android.os.Bundle;
import app.munch.facebookappevents.FacebookAppEventsPlugin;
import io.flutter.app.FlutterActivity;
import io.flutter.plugins.GeneratedPluginRegistrant;
public class MainActivity extends FlutterActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GeneratedPluginRegistrant.registerWith(this);
FacebookAppEventsPlugin.registerWith(this.registrarFor("io.flutter.plugins.facebookappevents.FacebookAppEventsPlugin"));
}
}
Step 4: Run your App
That’s all.
If you’re interested in implementing FacebookAppEvents for Android. Here is the gist: https://gist.github.com/Fuxingloh/4f565b683ba84914c20e090061f2ad80