Json-to-Dart-Model icon indicating copy to clipboard operation
Json-to-Dart-Model copied to clipboard

Support Map type

Open princ3od opened this issue 1 year ago • 6 comments

It would be great if Json-to-Dart-Model supported Map data type as it already supported List. Currrently, if the key is something like "key.Map":{}, it creates a whole new class called Map. I think it should use the existed Map class of Dart. Thank you.

princ3od avatar Aug 25 '22 10:08 princ3od

@princ3od Hi 👋 Thanks for suggestion. I wonder you mean MapView?

Example:

class Person extends MapView<String, String> { 
   final String firstName; 
   final String lastName; 
   Person(this.firstName, this.lastName) 
       : super( 
           { 
             'first_name': firstName, 
             'last_name': lastName, 
           }, 
         ); 
 }

source

iamarnas avatar Aug 25 '22 14:08 iamarnas

Thanks for replying @iamarnas. But I mean I have a json as show below

{
  "__className": "Topic",
  "id": "sport",
  "is_stored": true,
  "name": "Sport",
  "articles.Map": {}
}

My expected output will be

import 'package:freezed_annotation/freezed_annotation.dart';

part 'topic.freezed.dart';
part 'topic.g.dart';

@freezed
class Topic with _$Topic {
  factory Topic({
    String? id,
    String? name,
    @JsonKey(name: 'is_stored') bool? isStored,
    Map articles,
  }) = _Topic;

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

And the Map type should be Map class of Dart. Currently, it will create a new class called Map.

princ3od avatar Aug 26 '22 02:08 princ3od

@princ3od In this way "articles.Map": { ... } you force rename object type with this generator, but it just not work with primitive values. Have you tried it? I don't remember if Map is allowed. By using Freezed it would be possible to allow it.

iamarnas avatar Aug 26 '22 05:08 iamarnas

I tried it and it did not work. So how can I generate a class with a Map type field? I see List type field is already supported.

princ3od avatar Aug 26 '22 06:08 princ3od

I tried it and it did not work. So how can I generate a class with a Map type field? I see List type field is already supported.

If it doesn't work then it's not allowed. List supported becouse it generates with this generator but Map not. Need to implement.

@princ3od

iamarnas avatar Aug 26 '22 14:08 iamarnas

also looking for this. would be helpful for Firestore conversions.

clivi-kj avatar Aug 26 '22 23:08 clivi-kj