Skip to content
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

type 'String' is not a subtype of type 'Enum' in type cast #39

Open
nikitadol opened this issue May 20, 2022 · 4 comments
Open

type 'String' is not a subtype of type 'Enum' in type cast #39

nikitadol opened this issue May 20, 2022 · 4 comments
Labels
feature request New feature or request

Comments

@nikitadol
Copy link

Error:

Unhandled exception:
type 'String' is not a subtype of type 'TestEnum' in type cast
#0      _safeKeyFromJson (package:fast_immutable_collections/src/imap/imap.dart:1444:14)
#1      new IMap.fromJson.<anonymous closure> (package:fast_immutable_collections/src/imap/imap.dart:414:50)
#2      MapMixin.map (dart:collection/maps.dart:170:28)
#3      new IMap.fromJson (package:fast_immutable_collections/src/imap/imap.dart:413:12)
#4      _$TestClassFromJson (package:bug/main.g.dart:10:18)
#5      new TestClass.fromJson (package:bug/main.dart:25:7)
#6      main (package:bug/main.dart:9:13)

Minimal code:

dependencies:
  flutter:
    sdk: flutter

  fast_immutable_collections: ^7.3.1
  json_annotation: ^4.5.0

dev_dependencies:
  flutter_test:
    sdk: flutter

  build_runner: ^2.1.11
  json_serializable: ^6.2.0
import 'dart:convert';

import 'package:fast_immutable_collections/fast_immutable_collections.dart';
import 'package:json_annotation/json_annotation.dart';

part 'main.g.dart';

void main() {
  TestClass.fromJson(jsonDecode(jsonEncode(const TestClass(
    testField: IMapConst({
      TestEnum.testValue: true,
    }),
  ))));
}

@JsonSerializable()
class TestClass {
  final IMap<TestEnum, bool> testField;

  const TestClass({
    required this.testField,
  });

  factory TestClass.fromJson(Map<String, Object?> json) =>
      _$TestClassFromJson(json);

  Map<String, Object?> toJson() => _$TestClassToJson(this);
}

enum TestEnum {
  testValue,
}
@bryantgtx
Copy link

I was just coming in to report this, thanks. My map's key is an enum as well, so pretty much the exact case.

@marcglasberg
Copy link
Owner

Hello! Thank you for the report. JSON serialization and deserialization was kindly contributed by @knaeckeKami . Maybe he can help you. Or if you know how to fix this, I'm accepting PRs.

The code that needs to be improved is at the very end of this file:
https://github.com/marcglasberg/fast_immutable_collections/blob/master/lib/src/imap/imap.dart

Meanwhile, I have published version 7.4.1, which improves the error message. It will now be an unsupported error: JSON deserialization of IMap keys of type $type are not supported at the moment.

Also, created a test for that, at the very end of this file:

https://github.com/marcglasberg/fast_immutable_collections/blob/master/json_serializable_e2e_test/test/json_serialization_and_deserialization_test.dart

@nikitadol
Copy link
Author

Hi @marcglasberg

All keys in JSON are strings, so I don't understand why this code is here

Also, why do the _safeKeyFromJson and _safeKeyToJson methods exist?

factory IMap.fromJson(
Map<String, Object?> json,
K Function(Object?) fromJsonK,
V Function(Object?) fromJsonV,
) =>
json
.map<K, V>(
(key, value) => MapEntry(fromJsonK(_safeKeyFromJson<K>(key)), fromJsonV(value)))
.lock;

@knaeckeKami
Copy link
Contributor

knaeckeKami commented May 22, 2022

Hm, I don't see a way how this could be handled without a complete overhaul of serialization.

But you can try using json_serializable_fic (https://pub.dev/packages/json_serializable_fic ). It says discontinued because I thought with the addition of the toJson, fromJson methods this package should be obsolete, but it should still work.

There is a test for enum maps (https://github.com/knaeckeKami/json_serializable_immutable_collections/blob/master/builders/json_serializable_fic/test/integration/model_test.dart ).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants