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

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

Open
hmshohrab opened this issue Oct 14, 2024 · 2 comments
Open

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

hmshohrab opened this issue Oct 14, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@hmshohrab
Copy link

hmshohrab commented Oct 14, 2024

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 windows7lake added the enhancement New feature or request label Oct 14, 2024
@windows7lake
Copy link
Owner

@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
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants