Skip to content

If list is null, it will throw exception. #583

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
JamesKarl opened this issue Dec 31, 2019 · 7 comments
Closed

If list is null, it will throw exception. #583

JamesKarl opened this issue Dec 31, 2019 · 7 comments

Comments

@JamesKarl
Copy link

Dart code.

@JsonSerializable()
class StoreModel {
final List<String> imageUrls;
}

Generated Code.

StoreModel _$StoreModelFromJson(Map<String, dynamic> json) {
  return StoreModel(
    (json['imageUrls'] as List)?.map((e) => e as String)?.toList()
  );
}

(json['imageUrls'] as List) may throw Exception if json['imageUrls'] is null.

@kevmoo
Copy link
Collaborator

kevmoo commented Jan 1, 2020

We really need this feature –  dart-lang/language#399

I'm worried about the ugly code that will be required without it 😕

@lukepighetti
Copy link

We can't null coalesce the result to ?? []?

@kevmoo
Copy link
Collaborator

kevmoo commented Jun 4, 2020

Did you try using a default value of a const [] – I forget if that works or not...

@kevmoo
Copy link
Collaborator

kevmoo commented Jun 4, 2020

Or maybe we just need to wait until null safety lands – it's coming along nicely!

@lukepighetti
Copy link

Thanks for the tip. Can't try it now but I'll reach out again if the issue arises. Can't wait for NNBD!

@kevmoo
Copy link
Collaborator

kevmoo commented Jun 4, 2020 via email

@kevmoo
Copy link
Collaborator

kevmoo commented Apr 5, 2021

I think this is fixed. Please reopen if not!

@kevmoo kevmoo closed this as completed Apr 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants