rrousselGit / freezed

Code generation for immutable classes that has a simple syntax/API without compromising on the features.
https://pub.dev/packages/freezed
1.93k stars 237 forks source link

fromJson with generic class that has a field with other generic class: RangeError (index): Invalid value: Only valid value is 0: -1 #887

Open Albert221 opened 1 year ago

Albert221 commented 1 year ago

Describe the bug A clear and concise description of what the bug is.

import 'package:freezed_annotation/freezed_annotation.dart';

part 'response_model.freezed.dart';
part 'response_model.g.dart';

@Freezed(genericArgumentFactories: true)
class StrapiPaginated<T> with _$StrapiPaginated<T> {
  const factory StrapiPaginated({
    required List<StrapiData<T>> data,
  }) = _StrapiPaginated<T>;

  factory StrapiPaginated.fromJson(
    Map<String, dynamic> json,
    T Function(Object?) fromJsonT,
  ) =>
      _$StrapiPaginatedFromJson<T>(json, fromJsonT);
}

@Freezed(genericArgumentFactories: true)
class StrapiData<T> with _$StrapiData<T> {
  const factory StrapiData({
    required int id,
    required T attributes,
  }) = _StrapiData<T>;

  factory StrapiData.fromJson(
    Map<String, dynamic> json,
    T Function(Object?) fromJsonT,
  ) =>
      _$StrapiDataFromJson<T>(json, fromJsonT);
}

Error

[INFO] ------------------------------------------------------------------------

[INFO] Starting Build

[INFO] Updating asset graph...
[INFO] Updating asset graph completed, took 0ms

[INFO] Running build...
[SEVERE] json_serializable on lib/data/api/response_model.dart:

RangeError (index): Invalid value: Only valid value is 0: -1
[INFO] Running build completed, took 71ms

[INFO] Caching finalized dependency graph...
[INFO] Caching finalized dependency graph completed, took 8ms

[SEVERE] Failed after 80ms

Expected behavior Proper fromJson created

Probably related to #882.

ztaylor54 commented 12 months ago

Looks like this is the same issue I'm facing here: https://github.com/rrousselGit/freezed/issues/766#issuecomment-1811414548

mkhtradm01 commented 11 months ago

Sadly, I have the same issue here as well.

Rukkaitto commented 3 months ago

Same issue on my end :

import 'package:freezed_annotation/freezed_annotation.dart';

part 'attempt_result.freezed.dart';
part 'attempt_result.g.dart';

@freezed
class AttemptResult with _$AttemptResult {
  factory AttemptResult({
    required bool isCorrect,
    required AttemptResultProperty<String> name,
  }) = _AttemptResult;

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

@Freezed(genericArgumentFactories: true)
class AttemptResultProperty<T> with _$AttemptResultProperty<T> {
  factory AttemptResultProperty({
    required String state,
    required T value,
  }) = _AttemptResultProperty<T>;

  factory AttemptResultProperty.fromJson(
          Map<String, dynamic> json, T Function(Object?) fromJsonT) =>
      _$AttemptResultPropertyFromJson(json, fromJsonT);
}
[SEVERE] json_serializable on lib/core/models/attempt_result.dart:

RangeError (index): Invalid value: Only valid value is 0: -1
dart:core                                                           _Array.[]
package:json_serializable/src/type_helpers/json_helper.dart 161:39  _helperParams
package:json_serializable/src/type_helpers/json_helper.dart 51:9    JsonHelper.serialize
package:json_serializable/src/type_helper_ctx.dart 56:31            TypeHelperCtx.serialize.<fn>
dart:core                                                           Iterable.firstWhere
package:json_serializable/src/type_helper_ctx.dart 88:46            TypeHelperCtx._run
package:json_serializable/src/type_helper_ctx.dart 53:64            TypeHelperCtx.serialize
package:json_serializable/src/encoder_helper.dart 206:12            EncodeHelper._serializeField
package:json_serializable/src/encoder_helper.dart 134:42            EncodeHelper._writeToJsonSimple.<fn>
dart:core                                                           StringBuffer.writeAll
package:json_serializable/src/encoder_helper.dart 131:9             EncodeHelper._writeToJsonSimple
package:json_serializable/src/encoder_helper.dart 107:7             EncodeHelper.createToJson
dart:_internal                                                      WhereIterator.moveNext
package:json_serializable/src/json_part_builder.dart 64:27          _UnifiedGenerator.generate
package:source_gen/src/builder.dart 342:23                          _generate
dart:async                                                          Stream.toList.<fn>
package:source_gen/src/builder.dart 107:9                           _Builder._generateForLibrary
package:source_gen/src/builder.dart 99:5                            _Builder.build
longtn-imt commented 3 months ago

Same issue here

import 'package:freezed_annotation/freezed_annotation.dart';

part 'base_response.freezed.dart';
part 'base_response.g.dart';

@Freezed(genericArgumentFactories: true)
class SBaseResponse<T, E> with _$SBaseResponse<T, E> {
  const factory SBaseResponse({
    required SResponseStatus<E> responseStatus,
    required T results,
  }) = _SBaseResponse;

  const SBaseResponse._();

  factory SBaseResponse.fromJson(
    Map<String, dynamic> json,
    T Function(Object?) fromJsonT,
    E Function(Object?) fromJsonE,
  ) =>
      _$SBaseResponseFromJson(json, fromJsonT, fromJsonE);
}

@Freezed(genericArgumentFactories: true)
class SResponseStatus<E> with _$SResponseStatus<E> {
  const factory SResponseStatus({
    String? errorCode,
    String? message,
    E? errors,
  }) = _SResponseStatus;

  const SResponseStatus._();

  factory SResponseStatus.fromJson(
    Map<String, Object?> json,
    E Function(Object? json) fromJsonE,
  ) =>
      _$SResponseStatusFromJson(json, fromJsonE);
}
[SEVERE] json_serializable on lib/src/models/base_response.dart:

RangeError (index): Invalid value: Only valid value is 0: -1
dart:core                                                           _Array.[]
package:json_serializable/src/type_helpers/json_helper.dart 161:39  _helperParams
package:json_serializable/src/type_helpers/json_helper.dart 51:9    JsonHelper.serialize
package:json_serializable/src/type_helper_ctx.dart 56:31            TypeHelperCtx.serialize.<fn>
dart:core                                                           Iterable.firstWhere
package:json_serializable/src/type_helper_ctx.dart 88:46            TypeHelperCtx._run
package:json_serializable/src/type_helper_ctx.dart 53:64            TypeHelperCtx.serialize
package:json_serializable/src/encoder_helper.dart 206:12            EncodeHelper._serializeField
package:json_serializable/src/encoder_helper.dart 134:42            EncodeHelper._writeToJsonSimple.<fn>
dart:core                                                           StringBuffer.writeAll
package:json_serializable/src/encoder_helper.dart 131:9             EncodeHelper._writeToJsonSimple
package:json_serializable/src/encoder_helper.dart 107:7             EncodeHelper.createToJson
dart:_internal                                                      WhereIterator.moveNext
package:json_serializable/src/json_part_builder.dart 64:27          _UnifiedGenerator.generate
package:source_gen/src/builder.dart 342:23                          _generate
dart:async                                                          Stream.toList.<fn>
package:source_gen/src/builder.dart 107:9                           _Builder._generateForLibrary
package:source_gen/src/builder.dart 99:5                            _Builder.build
DevAdeel commented 2 months ago

Same issue here. Did you find any solution?

import 'package:freezed_annotation/freezed_annotation.dart';

part 'api_response.freezed.dart';
part 'api_response.g.dart';

@Freezed(genericArgumentFactories: true)
class APIResponse<T> with _$APIResponse<T> {
  factory APIResponse.fromJson(
          Map<String, dynamic> json, T Function(Object? json) fromJsonT) =>
      _$APIResponseFromJson<T>(json, fromJsonT);

  @JsonSerializable(explicitToJson: true, genericArgumentFactories: true)
  const factory APIResponse({
    required bool success,
    required APIResult<T> result,
    String? message,
  }) = _APIResponse;
}

@Freezed(genericArgumentFactories: true)
class APIResult<R> with _$APIResult<R> {
  factory APIResult.fromJson(
          Map<String, dynamic> json, R Function(Object? json) fromJsonR) =>
      _$APIResultFromJson<R>(json, fromJsonR);

  @JsonSerializable(explicitToJson: true, genericArgumentFactories: true)
  const factory APIResult({
    R? data,
  }) = _APIResult;
}
RangeError (length): Invalid value: Only valid value is 0: -1
dart:core                                                           _Array.[]
package:json_serializable/src/type_helpers/json_helper.dart 161:39  _helperParams
package:json_serializable/src/type_helpers/json_helper.dart 51:9    JsonHelper.serialize
package:json_serializable/src/type_helper_ctx.dart 56:31            TypeHelperCtx.serialize.<fn>
dart:core                                                           Iterable.firstWhere
package:json_serializable/src/type_helper_ctx.dart 88:46            TypeHelperCtx._run
package:json_serializable/src/type_helper_ctx.dart 53:64            TypeHelperCtx.serialize
package:json_serializable/src/encoder_helper.dart 206:12            EncodeHelper._serializeField
package:json_serializable/src/encoder_helper.dart 134:42            EncodeHelper._writeToJsonSimple.<fn>
dart:core                                                           StringBuffer.writeAll
package:json_serializable/src/encoder_helper.dart 131:9             EncodeHelper._writeToJsonSimple
package:json_serializable/src/encoder_helper.dart 107:7             EncodeHelper.createToJson
dart:_internal                                                      WhereIterator.moveNext
package:json_serializable/src/json_part_builder.dart 64:27          _UnifiedGenerator.generate
package:source_gen/src/builder.dart 342:23                          _generate
dart:async                                                          Stream.toList.<fn>
package:source_gen/src/builder.dart 107:9                           _Builder._generateForLibrary
package:source_gen/src/builder.dart 99:5                            _Builder.build