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
The reply handling in generated Dart code is extremely repetitive; every method contains this logic, verbatim:
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 finalelse { return ... }just replaced withreturn ...since there's no need for an else when throwing), dramatically reducing the amount of generated code./cc @gaaclarke