windows7lake / json2dart

Android Studio plugin use for prasing json to dart class with safe type convert.
5 stars 0 forks source link

The list must add default values ​​or nullable. #4

Open hmshohrab opened 1 month ago

hmshohrab commented 1 month ago

Need to add list default value or nullable. Json sample: { "userImage": "Shohrab", "socialMediaInfoess": [ { "ProfileID": 2, "SocialMediaURL": "http://facebook.com" } ] }

Output sample: class ProfileInfo { final String userImage; final List socialMediaInfoess;

ProfileInfo({ this.userImage = "", required this.socialMediaInfoess, //remove required and add default value or nullable (if possible) });

factory ProfileInfo.fromJson(Map<String, dynamic>? json) => ProfileInfo( userImage: asT(json, 'userImage'), socialMediaInfoess: asT(json, 'socialMediaInfoess').map((e) => SocialMediaInfoessItem.fromJson(e)).toList(), );

Map<String, dynamic> toJson() => { 'userImage': userImage, 'socialMediaInfoess': socialMediaInfoess.map((e) => e.toJson()).toList(), };

ProfileInfo copyWith({ String? userImage, List? socialMediaInfoess, }) { return ProfileInfo( userImage: userImage ?? this.userImage, socialMediaInfoess: socialMediaInfoess ?? this.socialMediaInfoess, ); } }

class SocialMediaInfoessItem { final int profileID; final String socialMediaURL;

SocialMediaInfoessItem({ this.profileID = 0, this.socialMediaURL = "", });

factory SocialMediaInfoessItem.fromJson(Map<String, dynamic>? json) => SocialMediaInfoessItem( profileID: asT(json, 'ProfileID'), socialMediaURL: asT(json, 'SocialMediaURL'), );

Map<String, dynamic> toJson() => { 'ProfileID': profileID, 'SocialMediaURL': socialMediaURL, };

SocialMediaInfoessItem copyWith({ int? profileID, String? socialMediaURL, }) { return SocialMediaInfoessItem( profileID: profileID ?? this.profileID, socialMediaURL: socialMediaURL ?? this.socialMediaURL, ); } }

windows7lake commented 1 month ago

@hmshohrab Deselect Enable Null Safety, and list can be nullable

image
class ProfileInfo {
  final String userImage;
  final List<SocialMediaInfoessItem>? socialMediaInfoess;

  ProfileInfo({
    this.userImage = "",
    this.socialMediaInfoess,
  });

  factory ProfileInfo.fromJson(Map<String, dynamic> json) => ProfileInfo(
    userImage: asString(json, 'userImage'),
    socialMediaInfoess: asList(json, 'socialMediaInfoess').map((e) => SocialMediaInfoessItem.fromJson(e)).toList(),
  );

  Map<String, dynamic> toJson() => {
    'userImage': userImage,
    'socialMediaInfoess': socialMediaInfoess?.map((e) => e.toJson()).toList(),
  };
}

class SocialMediaInfoessItem {
  final int profileID;
  final String socialMediaURL;

  SocialMediaInfoessItem({
    this.profileID = 0,
    this.socialMediaURL = "",
  });

  factory SocialMediaInfoessItem.fromJson(Map<String, dynamic> json) => SocialMediaInfoessItem(
    profileID: asInt(json, 'ProfileID'),
    socialMediaURL: asString(json, 'SocialMediaURL'),
  );

  Map<String, dynamic> toJson() => {
    'ProfileID': profileID,
    'SocialMediaURL': socialMediaURL,
  };
}
windows7lake commented 1 month ago

json2dart-1.2.4.zip 1.2.4 support to add default value for list type property in construction method.