Skip to content

[WIP] Extension methods support #658

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed

Conversation

trevorwang
Copy link

@trevorwang
Copy link
Author

trevorwang commented Jun 18, 2020

This broke all tests which contain json encode

The extension methods are not treated as _toEncodable functions

https://github.com/dart-lang/sdk/blob/b43714854e4d68907ab2dd033353e234a690cc6e/sdk/lib/convert/json.dart#L521

extension PersonExt on Person {
  static Person fromJson(Map<String, dynamic> json) => _$PersonFromJson(json);
  Map<String, dynamic> toJson() => _$PersonToJson(this);
}
NoSuchMethodError (NoSuchMethodError: Class 'Person' has no instance method 'toJson'.
Receiver: Instance of 'Person'
Tried calling: toJson())

@trevorwang
Copy link
Author

trevorwang commented Jun 18, 2020

Add test code here

import 'package:test/test.dart';

void main() {
  test('toJson() in extension is not visible', () async {
    final demo = Demo();
    expect(_defaultToEncodable(demo), demo.toJson());
  });
}

dynamic _defaultToEncodable(dynamic object) => object.toJson();

class Demo {}

extension DemoExt on Demo {
  Map<String, dynamic> toJson() => <String, dynamic>{'foo': 'bar'};
}

And here's the error message

Exception has occurred.
NoSuchMethodError (NoSuchMethodError: Class 'Demo' has no instance method 'toJson'.
Receiver: Instance of 'Demo'
Tried calling: toJson())

@trevorwang
Copy link
Author

Close first due to #558 (comment)

@trevorwang trevorwang closed this Jun 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant