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

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

Closed
4 tasks done
Abdelazeem777 opened this issue Dec 20, 2021 · 3 comments
Closed
4 tasks done
Labels
Dart Target language: Dart feature-request New feature or request model-gen Issues on datastore model generation

Comments

@Abdelazeem777
Copy link

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

@phani-srikar phani-srikar added feature-request New feature or request Dart Target language: Dart model-gen Issues on datastore model generation labels Dec 20, 2021
@phani-srikar
Copy link
Contributor

Hi @Abdelazeem777 Thanks for bringing this to our attention. We will discuss supporting this for Dart Models internally and get back on this.

@dpilch
Copy link
Member

dpilch commented Apr 4, 2024

Resolved in #491

@dpilch dpilch closed this as completed Apr 4, 2024
Copy link

github-actions bot commented Apr 4, 2024

This issue is now closed. Comments on closed issues are hard for our team to see.
If you need more assistance, please open a new issue that references this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Dart Target language: Dart feature-request New feature or request model-gen Issues on datastore model generation
Projects
None yet
Development

No branches or pull requests

3 participants