Skip to content

Add default methodCallHandler to MethodChannel's const constructor #34429

@tymur-l

Description

@tymur-l

When a project with a plugin is beign created, the sample plugin class consists of:

// the platform channel itself
static const MethodChannel _channel = const MethodChannel('channel_name');

// methods that call platform
static ReturnType methodName(/*arguments, if any*/) { /*code that calls platform*/ }

I would like to set a method call handler on _channel, but since it is static const I currently have no means of doing that. The solution I am using looks as follows:

static final MethodChannel _channel = getPluginChannel();

static MethodChannel _getPluginChannel()
{
  final MethodChannel resultChannel = MethodChannel('channel_name');
    resultChannel.setMethodCallHandler((MethodCall call) {
      /*code*/
    });
  return resultChannel;
}

The drawback here is that the _channel variable is not const and there is no way to make it const.
Is it possible to add Future<dynamic> handler(MethodCall call) optional argument to the const constructor of the MethodChannel class?

Metadata

Metadata

Assignees

No one assigned

    Labels

    c: new featureNothing broken; request for a new capabilityc: proposalA detailed proposal for a change to Flutterframeworkflutter/packages/flutter repository. See also f: labels.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions