Json_serializable.dart: type 'List<dynamic>' is not a subtype of type 'List<int>' in type cast

Created on 5 Jun 2020  Â·  13Comments  Â·  Source: google/json_serializable.dart

      waveform: $checkedConvert(json, 'waveform',
          (v) => const ByteArrayJsonConverter().fromJson(v as List<int>)),
class ByteArrayJsonConverter implements JsonConverter<Uint8List, List<int>> {
  const ByteArrayJsonConverter();

  @override
  Uint8List fromJson(List<int> json) => Uint8List.fromList(json);

  @override
  List<int> toJson(Uint8List list) => list == null ? [] : List<int>.from(list);
}
Could not create `FullTrack`.
There is a problem with "waveform".
type 'List<dynamic>' is not a subtype of type 'List<int>' in type cast

This is the manual fix:

      waveform: $checkedConvert(
          json, 'waveform', (v) => const ByteArrayJsonConverter().fromJson((v as List<dynamic>).cast<int>())),
needs info

Most helpful comment

I am having the same issue in combination with another package call "artemis"
(https://github.com/comigor/artemis/)

It uses json_serilizable under the hood and the generated code looks like this:

// from fromJson method.
    ..authorIds =
        fromGraphQLListUuidToDartListString(json['authorIds'] as List<String>)
    ..genreIds =
        fromGraphQLListUuidToDartListString(json['genreIds'] as List<String>)

At runtime, this crashes with the error type 'List<dynamic>' is not a subtype of type 'List<String>' in type cast.

My pubspec.yaml:

# Maybe dart 2.13.0 is causing this issue?
environment:
  sdk: ">=2.13.0 <3.0.0"

dev_dependencies:
  build_runner: ^2.0.4
  extra_pedantic: 1.3.0
  json_serializable: 4.1.3
  test: ^1.17.5

https://github.com/comigor/artemis/blob/beta/pubspec.yaml
We are using the beta version of artemis which uses json_annotation: ^4.0.1 and json_serializable: ^4.1.1 internally.
Source: https://github.com/comigor/artemis/blob/beta/pubspec.yaml

All 13 comments

Another fix:

class ByteArrayJsonConverter implements JsonConverter<Uint8List, List<dynamic>> {
  const ByteArrayJsonConverter();

  @override
  Uint8List fromJson(List<dynamic> json) => Uint8List.fromList(json.cast<int>());

  @override
  List<dynamic> toJson(Uint8List list) => list == null ? [] : List<dynamic>.from(list);
}

Could you explain the issue a bit better? Unclear what the problem is...

I just ran into the same issue. For some reason, json_serializable suddenly removed the type cast in this scenario:

// before
return ServerGeneralConfig(
  redirectHosts:
    (json['redirectHosts'] as List).map((e) => e as String).toList(),
  // ...
);
// after
return ServerGeneralConfig(
    redirectHosts: json['redirectHosts'],
    // ...
);

The class is setup like this:

  @JsonKey(disallowNullValue: true)
  final List<String> redirectHosts;

@enyo – explain "suddenly". Did you update any packages?

I just ran this through:

@JsonSerializable()
class I652 {
  @JsonKey(disallowNullValue: true)
  final List<String> redirectHosts;

  I652(this.redirectHosts);
}

And got this output – which looks great to me.

I652 _$I652FromJson(Map<String, dynamic> json) {
  $checkKeys(json, disallowNullValues: const ['redirectHosts']);
  return I652(
    (json['redirectHosts'] as List)?.map((e) => e as String)?.toList(),
  );
}

@kevmoo sorry. Yes after upgrading dart version to 2.9. I haven't changed the json_serializable Version though.

Can I provide additional info or do some tests?

EDIT: I'm trying to isolate it in a new repo that I can share with you.

Ok, so I've found a way to reproduce it, but it has apparently already been fixed.

On version [email protected] this file would not properly cast the list:

import 'dart:io'; // <---- This is the offending line

import 'package:json_annotation/json_annotation.dart';

part 'message.g.dart';

@JsonSerializable()
class Message {
  @JsonKey()
  final List<String> myStrings;

  Message({
    this.myStrings,
  });
}

Whenever there is a dart:io import it wouldn't cast it, when I removed the import it worked. (Obviously, in my real-life scenario, there was a reason to include it).

I have upgraded to [email protected] in my test repo, and that solved the issue, and I'm going to attempt the same in my company's repo.

I think that it's some kind of analysis server dependency that is the culprit.

EDIT: Can confirm that upgrading all libraries solved the issue for me.

@enyo – that is SUPER weird! I'll contact the right folks

@kevmoo yeah it's a really strange behaviour! But don't bother anybody. Who knows which funky versions I had combined to get this behaviour. On the latest versions everything is fine!

Seems like there is a fix. Closing out!

i am facing a problem
can any one help me with this problem
//collect all of the odd values in a array recursion

main() {
print(collectoddValues([1,2,3,4,5,6,7,89]))
;}

collectoddValues( arr) {
var result = [] ;

helper(helperInput) {
if (helperInput.length == 0) {
return 0;
}
if (helperInput[0] % 2 != 0) {
result.insert(helperInput, [0]);
}
return helper(helperInput.substring(1));
}

helper(arr);
return result;
}
//
$ dart helpermethodrecursion.dart
Unhandled exception:
type 'List' is not a subtype of type 'int'

0 collectoddValues.helper (file:///G:/DataStructurs/DartAlgorithm/recursion/helpermethodrecursion.dart:15:21)

1 collectoddValues (file:///G:/DataStructurs/DartAlgorithm/recursion/helpermethodrecursion.dart:20:9)

2 main (file:///G:/DataStructurs/DartAlgorithm/recursion/helpermethodrecursion.dart:4:9)

3 _startIsolate. (dart:isolate-patch/isolate_patch.dart:307:19)

4 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:174:12)

@vipuluthaiah – you should ask this type of question on StackOverflow 😄

I am having the same issue in combination with another package call "artemis"
(https://github.com/comigor/artemis/)

It uses json_serilizable under the hood and the generated code looks like this:

// from fromJson method.
    ..authorIds =
        fromGraphQLListUuidToDartListString(json['authorIds'] as List<String>)
    ..genreIds =
        fromGraphQLListUuidToDartListString(json['genreIds'] as List<String>)

At runtime, this crashes with the error type 'List<dynamic>' is not a subtype of type 'List<String>' in type cast.

My pubspec.yaml:

# Maybe dart 2.13.0 is causing this issue?
environment:
  sdk: ">=2.13.0 <3.0.0"

dev_dependencies:
  build_runner: ^2.0.4
  extra_pedantic: 1.3.0
  json_serializable: 4.1.3
  test: ^1.17.5

https://github.com/comigor/artemis/blob/beta/pubspec.yaml
We are using the beta version of artemis which uses json_annotation: ^4.0.1 and json_serializable: ^4.1.1 internally.
Source: https://github.com/comigor/artemis/blob/beta/pubspec.yaml

Was this page helpful?
0 / 5 - 0 ratings