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

missing generic type in .fromJson #32

Closed
smiLLe opened this issue Feb 13, 2020 · 2 comments
Closed

missing generic type in .fromJson #32

smiLLe opened this issue Feb 13, 2020 · 2 comments
Labels
bug Something isn't working

Comments

@smiLLe
Copy link
Contributor

smiLLe commented Feb 13, 2020

after creating these model classes on version 0.2.4...

/// model.dart

import 'package:json_annotation/json_annotation.dart';
import 'package:flutter/foundation.dart';

part 'model.freezed.dart';
part 'model.g.dart';

@immutable
abstract class Parent<T> with _$Parent<T> {
  const factory Parent(@DataConverter() List<T> items) = _Parent<T>;

  factory Parent.fromJson(Map<String, dynamic> json) =>
      _$ParentFromJson<T>(json);
}

@immutable
abstract class Item with _$Item {
  const factory Item(String name) = _Item;

  factory Item.fromJson(Map<String, dynamic> json) => _$ItemFromJson(json);
}

class DataConverter<T> implements JsonConverter<T, Object> {
  const DataConverter();

  @override
  T fromJson(Object json) {
    return Item.fromJson(json) as T;
  }

  @override
  Object toJson(T object) {
    return object;
  }
}

... i am going to run the default sample project. ( flutter create ). It will result in the following error:

lib/model.freezed.dart:74:57: Error: The constructor function type '_$_Parent<dynamic> Function(Map<String, dynamic>)' isn't a subtype of '_Parent<T> Function(Map<String, dynamic>)'.
 - '_$_Parent' is from 'package:freezed_test/model.dart' ('lib/model.dart').
 - 'Map' is from 'dart:core'.
 - '_Parent' is from 'package:freezed_test/model.dart' ('lib/model.dart').
  factory _Parent.fromJson(Map<String, dynamic> json) = _$_Parent.fromJson;

I can fix the error by opening model.freezed and change

/// model.freezed.dart:74

factory _Parent.fromJson(Map<String, dynamic> json) = _$_Parent.fromJson;
to
factory _Parent.fromJson(Map<String, dynamic> json) = _$_Parent<T>.fromJson;
@rrousselGit rrousselGit added the bug Something isn't working label Feb 13, 2020
@rrousselGit
Copy link
Owner

Fixed in 0.2.5 😄

@smiLLe
Copy link
Contributor Author

smiLLe commented Feb 13, 2020

I will try it immediately :) Thanks for the fast response!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants