Skip to content

List<int> becomes List<String> in toJson() method #40

@jeecrypt

Description

@jeecrypt

List becomes List in toJson() method

library version: 3.2.4

My meal_day.json file:

{
"id": 1,
"day": 1,
"recipes": [0]
}

Model after generate (flutter packages pub run json_to_model):

import 'package:quiver/core.dart';

import 'index.dart';

@immutable
class MealDay {

  const MealDay({
    required this.id,
    required this.day,
    required this.recipes,
  });

  final int id;
  final int day;
  final List<int> recipes;

  factory MealDay.fromJson(Map<String,dynamic> json) => MealDay(
    id: json['id'] as int,
    day: json['day'] as int,
    recipes: (json['recipes'] as List? ?? []).map((e) => e as int).toList()
  );
  
  Map<String, dynamic> toJson() => {
    'id': id,
    'day': day,
    'recipes': recipes.map((e) => e.toString()).toList() // this toString !!!!!!!!!!!!!!
  };

  MealDay clone() => MealDay(
    id: id,
    day: day,
    recipes: recipes.toList()
  );


  MealDay copyWith({
    int? id,
    int? day,
    List<int>? recipes
  }) => MealDay(
    id: id ?? this.id,
    day: day ?? this.day,
    recipes: recipes ?? this.recipes,
  );

  @override
  bool operator ==(Object other) => identical(this, other)
    || other is MealDay && id == other.id && day == other.day && recipes == other.recipes;

  @override
  int get hashCode => id.hashCode ^ day.hashCode ^ recipes.hashCode;
}

Problen in to metod toJson() !

To fix i replace from
'recipes': recipes.map((e) => e.toString()).toList()

to
'recipes': recipes.map((e) => e).toList()

Upd:
In source code 'dart_declaration.dart' replace
this:

String toJsonBody(String className) {
    return checkNestedTypes(type!, (String cleanedType, bool isList, bool isListInList, bool isModel) {
      String conversion;

      if (isListInList) {
        conversion = '$name$isNullableString.map((e) => e.map((e) => e.toJson()).toList()).toList()';
      } else if (isList) {
        if (isModel) {
          conversion = '$name$isNullableString.map((e) => e.toJson()).toList()';
        } else {
          conversion = '$name$isNullableString.map((e) => e.toString()).toList()';
        }
      } else if (isModel) {
        conversion = '$name$isNullableString.toJson()';
      } else if (isDatetime && !isTimeStamp) {
        conversion = '$name$isNullableString.toIso8601String()';
      } else if (isTimeStamp) {
        conversion = '$name$isNullableString.millisecondsSinceEpoch';
      } else {
        conversion = name ?? '';
      }

      return "'$originalName': $conversion";
    });
  }

to this:

String toJsonBody(String className) {
    return checkNestedTypes(type!, (String cleanedType, bool isList, bool isListInList, bool isModel) {
      String conversion;

      if (isListInList) {
        conversion = '$name$isNullableString.map((e) => e.map((e) => e.toJson()).toList()).toList()';
      } else if (isList) {
        if (isModel) {
          conversion = '$name$isNullableString.map((e) => e.toJson()).toList()';
        } else {
          conversion = '$name$isNullableString.map((e) => e).toList()';
        }
      } else if (isModel) {
        conversion = '$name$isNullableString.toJson()';
      } else if (isDatetime && !isTimeStamp) {
        conversion = '$name$isNullableString.toIso8601String()';
      } else if (isTimeStamp) {
        conversion = '$name$isNullableString.millisecondsSinceEpoch';
      } else {
        conversion = name ?? '';
      }

      return "'$originalName': $conversion";
    });
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions