Skip to content

[pigeon] Reduce duplication in generated Dart code. #101446

@stuartmorgan-g

Description

@stuartmorgan-g

The reply handling in generated Dart code is extremely repetitive; every method contains this logic, verbatim:

  if (replyMap == null) {
    throw PlatformException(
      code: 'channel-error',
      message: 'Unable to establish connection on channel.',
    );
  } else if (replyMap['error'] != null) {
    final Map<Object?, Object?> error =
        (replyMap['error'] as Map<Object?, Object?>?)!;
    throw PlatformException(
      code: (error['code'] as String?)!,
      message: error['message'] as String?,
      details: error['details'],
    );
  }

See https://github.com/flutter/plugins/blob/709b3a887e76d8851112a2c29d9407727c0db8f2/packages/webview_flutter/webview_flutter_wkwebview/lib/src/common/web_kit.pigeon.dart for an example; it's a 1500 line file and a third of those lines are copies of this logic.

It can be trivial extracted to a void _validateReply(Map<Object?, Object>? reply) helper (and the final else { return ... } just replaced with return ... since there's no need for an else when throwing), dramatically reducing the amount of generated code.

/cc @gaaclarke

Metadata

Metadata

Assignees

Labels

P2Important issues not at the top of the work listp: pigeonrelated to pigeon messaging codegen toolpackageflutter/packages repository. See also p: labels.team-ecosystemOwned by Ecosystem teamtriaged-ecosystemTriaged by Ecosystem team

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions