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

Support Nullable Types on props | Flutter 2.0 and Dart 2.12 upgrade #111

Closed
Edorin9 opened this issue Mar 4, 2021 · 13 comments
Closed

Support Nullable Types on props | Flutter 2.0 and Dart 2.12 upgrade #111

Edorin9 opened this issue Mar 4, 2021 · 13 comments
Assignees
Labels
question Further information is requested
Projects

Comments

@Edorin9
Copy link

Edorin9 commented Mar 4, 2021

Describe the bug
The props override property of an equatable doesn't support nullable (eg: String?) values.

To Reproduce
Steps to reproduce the behavior:

  1. Upgrade flutter on stable channel (flutter channel stable && flutter upgrade)
  2. Upgrade dart sdk version to sdk: ">=2.12.0 <3.0.0"
  3. Create an equatable with a nullable property (final String? name)
  4. Pass the 'name' property in the equatable props array
@override
  List<Object> get props => [name];

Expected behavior
No IDE errors/build errors should show up

A possible workaround on my side is to
Assign a fallback value / ternary, which I would say is not reecommended

@override
  List<Object> get props => [name ?? ''];

Additional context
The element type 'String?' can't be assigned to the list type 'Object'.

@felangel felangel self-assigned this Mar 4, 2021
@felangel felangel added question Further information is requested waiting for response Waiting for follow up labels Mar 4, 2021
@felangel
Copy link
Owner

felangel commented Mar 4, 2021

Hi @Edorin9 👋
Thanks for opening an issue!

If you want to use nullable props you can just update the props override:

@override
List<Object?> get props => [name];

Hope that helps 👍

@felangel
Copy link
Owner

felangel commented Mar 4, 2021

@Edorin9 can this be closed or are you still having trouble with nullable props?

@Edorin9
Copy link
Author

Edorin9 commented Mar 4, 2021

Thanks for the reply @felangel. We can close it for now. I've actually downgraded flutter and quit upgrading a project, since there are other packages that needs updating as well.

@Edorin9 Edorin9 closed this as completed Mar 4, 2021
@felangel felangel removed the waiting for response Waiting for follow up label Mar 4, 2021
@felangel felangel added this to Done in equatable Mar 4, 2021
@JakeHadley
Copy link

JakeHadley commented Apr 28, 2021

@felangel I was having the same issue, with vscode, I added in the nullable operator (?) and I got this error from vscode...

('List<Object>? Function()') isn't a valid override of 'Equatable.props' ('List<Object?> Function()').dart(invalid_override)

Granted, my class currently doesn't have any props, so I just have List<Object>? get props => null; which causes this error. Do I need to override the get props if I don't have any props?

@felangel
Copy link
Owner

felangel commented Apr 28, 2021

@JakeHadley if you don’t have any props the override should be:

List<Object> get props => [];

If you have nullable props it should be:

List<Object?> get props => [propA, propB];

Hope that helps 👍

@JakeHadley
Copy link

JakeHadley commented Apr 28, 2021 via email

@luandnguyen
Copy link

@felangel Appreciate the solution. Should this be mentioned in the README?

@ariefwijaya
Copy link

@JakeHadley if you don’t have any props the override should be:

List<Object> get props => [];

If you have nullable props it should be:

List<Object?> get props => [propA, propB];

Hope that helps 👍

I can't make this work. I've tried List<Object?> but I got this 'NotificationNavigateScreen.props' ('List<Object?> Function()') isn't a valid override of 'NotificationState.props' ('List<Object> Function()')

@robyhuzwandar
Copy link

@JakeHadley if you don’t have any props the override should be:
List<Object> get props => [];
If you have nullable props it should be:
List<Object?> get props => [propA, propB];
Hope that helps 👍

I can't make this work. I've tried List<Object?> but I got this 'NotificationNavigateScreen.props' ('List<Object?> Function()') isn't a valid override of 'NotificationState.props' ('List<Object> Function()')

Hi @felangel how about that I got the error too

@felangel
Copy link
Owner

@JakeHadley if you don’t have any props the override should be:
List<Object> get props => [];
If you have nullable props it should be:
List<Object?> get props => [propA, propB];
Hope that helps 👍

I can't make this work. I've tried List<Object?> but I got this 'NotificationNavigateScreen.props' ('List<Object?> Function()') isn't a valid override of 'NotificationState.props' ('List<Object> Function()')

Hi @felangel how about that I got the error too

Can you share a minimal reproduction sample?

@ariefwijaya
Copy link

Sorry, my bad. I found it. I need to use List<Object?> in my abstract class when extending Equatable. Just because my abstract is not nullable, it will force the child to be non-nullable too.

@Ashrafe
Copy link

Ashrafe commented Aug 16, 2021

Sorry, my bad. I found it. I need to use List<Object?> in my abstract class when extending Equatable. Just because my abstract is not nullable, it will force the child to be non-nullable too.

yea that's it ty

@AnnaPS
Copy link

AnnaPS commented Dec 29, 2021

@JakeHadley if you don’t have any props the override should be:

List<Object> get props => [];

If you have nullable props it should be:

List<Object?> get props => [propA, propB];

Hope that helps 👍

Thank you Felix, I was looking for this solution :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
equatable
  
Done
Development

No branches or pull requests

8 participants