google / google/json_serializable.dart

Transforming empty nested map to object exception

Open
#1,388 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Dart
Stars
1.6k
Forks
461
Avg merge
45m
Merged PRs (30d)
1

Description

Following to this issue:
https://stackoverflow.com/questions/75522984/transforming-empty-nested-map-to-object-exception

I have the same problem.
But this problem happens only when I run a unit tests.
When I use the parse the same json in my app (Get the same json as dio response) it's ok,
But when I use the same json as mock value for unit tests, it throws an exception since there is object value {}

What is the best way to handle cases of empty objects?
Why does it happen only in the unit tests when I use the json object as mock value.

(When `e` value is `{}`, not null)
```
Location.fromJson(e as Map))
```

It comes from this:
(When `json['locations']` value is `[{}]`, not null)
From the generated file.
The problem is with the Location.fromJson
```
locations: (json['locations'] as List?)
?.map((e) => Location.fromJson(e as Map))
.toList(),
```
The main problem is with `e as Map` that behaves differently.

```
Unhandled exception:
type '_Map' is not a subtype of type 'Map' in type cast
```

Thank you in advance.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.