Skip to content

Dart models must include toMap&fromMap besides toJson&fromJson  #337

Closed
@Abdelazeem777

Description

@Abdelazeem777

Before opening, please confirm:

  • I have installed the latest version of the Amplify CLI (see above), and confirmed that the issue still persists.
  • I have searched for duplicate or closed issues.
  • I have read the guide for submitting bug reports.
  • I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.

How did you install the Amplify CLI?

npm

If applicable, what version of Node.js are you using?

v14.15.3

Amplify CLI Version

7.6.4

What operating system are you using?

Ubuntu

Amplify Codegen Command

codegen, codegen models

Describe the bug

The generated models code should include toJson&fromJson for normal json(String) instead of using them for (Map).

Also the (Map) class should have toMap&fromMap instead of using toJson&fromJson.

like this:

 Map<String, dynamic> toMap() {
    return {
      'id': id,
      'title': title,
      'icon': icon,
    });
  }

  factory Course.fromMap(Map<String, dynamic> map) {
    return Course(
      id: map['id'],
      title: map['title'],
      icon: map['icon'] ?? '',
      );
  }

  String toJson() => json.encode(toMap());

  factory Course.fromJson(String source) => Course.fromMap(json.decode(source));

Expected behavior

Have model like this:

 Map<String, dynamic> toMap() {
    return {
      'id': id,
      'title': title,
      'icon': icon,
    });
  }

  factory Course.fromMap(Map<String, dynamic> map) {
    return Course(
      id: map['id'],
      title: map['title'],
      icon: map['icon'] ?? '',
      );
  }

  String toJson() => json.encode(toMap());

  factory Course.fromJson(String source) => Course.fromMap(json.decode(source));

Reproduction steps

  1. just generate the models

GraphQL schema(s)

# Put schemas below this line

Log output

# Put your logs below this line


Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    DartTarget language: Dartfeature-requestNew feature or requestmodel-genIssues on datastore model generation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions