Json_serializable.dart: If list is null, it will throw exception.

Created on 31 Dec 2019  Â·  7Comments  Â·  Source: google/json_serializable.dart

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.

P2 medium bug

All 7 comments

We really need this feature –  https://github.com/dart-lang/language/issues/399

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

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

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

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

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

Migrating json_serializable to it is going to be a BEAR! I'm looking
forward to it, too

On Wed, Jun 3, 2020 at 8:04 PM Luke Pighetti notifications@github.com
wrote:

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

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/dart-lang/json_serializable/issues/583#issuecomment-638574020,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AAAEFCVQ75UMW5VOG4VQQODRU4FK7ANCNFSM4KBUKGYQ
.

I think this is fixed. Please reopen if not!

Was this page helpful?
0 / 5 - 0 ratings