Skip to content
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

Feature request: provide more information when an @required parameter is null #36524

Open
HansMuller opened this issue Apr 8, 2019 · 6 comments
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language).

Comments

@HansMuller
Copy link

It's common for @required named parameters to be non-null.

class Foo {
  Foo({ @required this.bar }) : assert(bar != null);
  final String bar;
}

void main() {
  Foo foo = Foo();
}

Currently, upon "hot restart", this idiomatic Flutter class fails with:

E/flutter (22230): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: 'package:test_issue/main.dart': Failed assertion: line 4 pos 40: 'bar != null': is not true.

It would be somewhat more informative if it failed with a message like:

E/flutter (22230): 'package:test_issue/main.dart': required Foo parameter bar must not be null, line 4 pos 40

For Flutter classes it might also be helpful if a link to the API doc for the required parameter was provided.

This issue was originally reported here: flutter/flutter#29046 (comment)

@matanlurey
Copy link
Contributor

Wouldn't you use the second field in assert to do this?

: assert(bar != null, 'Required parameter bar must not be null')

@bwilkerson bwilkerson added the area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). label Apr 8, 2019
@bwilkerson
Copy link
Member

Something to consider if we introduce an actual keyword to replace @required.

@eernstg
Copy link
Member

eernstg commented Apr 9, 2019

Cf. dart-lang/language#156 (comment), such a keyword could very well be introduced, and it would be implied (maybe without the keyword, maybe the keyword would be mandatory) that a named parameter with a non-null type and no default is required. It wouldn't make sense to require a named parameter with a default value to be passed, so it should most likely be an error to have that.

I think there wouldn't be an obvious justification for the converse rule: A named parameter whose type is nullable could be required and it could be optional, with and without a default value, all combinations will work and might make sense in the given application context (so we might as well allow developers to make the choice using an explicit required).

With that, this could be considered as yet another voice in favor of an explicit required modifier on parameters in function types and function declarations, that is, part of the discussion in dart-lang/language#156.

@HansMuller
Copy link
Author

@matanlurey you're right, we could generate a reasonable message (even a link to the API doc) by hand. There are loads of these, so it would be nice if it the reasonable message was generated by default.

@matanlurey
Copy link
Contributor

The issue here is @required is not a language feature, so the VM would not be parsing it and using it to fill in details of the error like you'd prefer. This could change if it becomes a keyword/language feature in the future.

@HansMuller: On a related note, I think your own team requested a lint to enforce adding messages?
https://dart-lang.github.io/linter/lints/prefer_asserts_with_message.html

@HansMuller
Copy link
Author

@matanlurey: yes we did request such a lint. Once that's landed we'll be asking for a lint that checks if the messages are actually helpful :-).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language).
Projects
None yet
Development

No branches or pull requests

4 participants