Search code examples
flutterflutter-pluginflutter-platform-channel

Naming a Flutter Plugin EventChannel


I created a new plugin with

flutter create --template plugin alfalfa

which generates lib/alfalfa.dart containing

import 'dart:async';

import 'package:flutter/services.dart';

class Alfalfa {
  static const MethodChannel _channel =
      const MethodChannel('alfalfa');

  //...
}

I want to add an EventChannel so Java and Objective-C code can call back to the Dart code. I don't know what the name of the EventChannel should be.

final EventChannel _eventChannel =
    const EventChannel("com.rollingfields.alfalfa/events");

or

final EventChannel _eventChannel =
    const EventChannel("alfalfa/events");

or something else? Is there a convention?

If the better option for the EventChannel is the name including the reverse domain, should I rename the generated MethodChannel to be com.rollingfields.alfalfa?


Solution

  • When in doubt, check the flutter plugins repo. The connectivity plugin uses:

      @visibleForTesting
      static const MethodChannel methodChannel = MethodChannel(
        'plugins.flutter.io/connectivity',
      );
    
      @visibleForTesting
      static const EventChannel eventChannel = EventChannel(
        'plugins.flutter.io/connectivity_status',
      );
    

    so, certainly one example of good practice. So, perhaps com.rollingfields/alfalfa and com.rollingfields/alfalfa_events