google / google/json_serializable.dart

I want to convert JSON data to a generic type in Flutter.

Open
#779 3 comments 12 reactions 0 assignees View on GitHub
Dominant language
Dart
Stars
1.6k
Forks
461
Avg merge
45m
Merged PRs (30d)
1

Description

Hello everyone I'm new to Flutter.
I want to convert JSON data to a generic type in Flutter.

```
class ServiceResult
{
T result;
String message;
String errorCode;
bool hasError;
bool hasSuccessMessage;
}
```

I can use
` Map`
But I don't want to use it dynamically. I want to cast to T.
I have always seen dynamic uses in my research. Is there a method that I can cast directly to T?

I get the following error from the code line below.
**_type '_InternalLinkedHashMap' is not a subtype of type 'UserDto' in type cast_**

```
ServiceResult fromJson(
Map json
) {
return ServiceResult()
..message = json['message'] as String
..errorCode = json['errorCode'] as String
..hasError = json['hasError'] as bool
..hasSuccessMessage = json['hasSuccessMessage'] as bool
..result = json['result'] as T;
}
```

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.