Dart-Data-Class-Generator icon indicating copy to clipboard operation
Dart-Data-Class-Generator copied to clipboard

from map has extra opening bracket

Open nitishk72 opened this issue 2 years ago • 2 comments

How to reproduce the error?

Please generate the model for this class

class User {
  String name;
  List<String> children;
}

The generated model will look something like this. In User.fromMap we have invalid code List<String>.from((map['children'] as List<String>)

There is one extra opening parentheses


import 'dart:convert';

class User {
  String name;
  List<String> children;
  User({
    required this.name,
    required this.children,
  });

  Map<String, dynamic> toMap() {
    return <String, dynamic>{
      'name': name,
      'children': children,
    };
  }

  factory User.fromMap(Map<String, dynamic> map) {
    return User(
      name: map['name'] as String,
      children: List<String>.from((map['children'] as List<String>),
    );
  }

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

  factory User.fromJson(String source) => User.fromMap(json.decode(source) as Map<String, dynamic>);
}

nitishk72 avatar Aug 27 '22 18:08 nitishk72