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

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

Open
jeecrypt opened this issue Jan 3, 2023 · 0 comments
Open

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

jeecrypt opened this issue Jan 3, 2023 · 0 comments

Comments

@jeecrypt
Copy link

jeecrypt commented Jan 3, 2023

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";
    });
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant