i read code somewhere from github and see 3 models here. this code should be use form get,post,put and delete but before go to far i dont understand why he separate 3 models. maybe someone can explain me what this person thingking. this is the models
user.dart
import 'package:json_annotation/json_annotation.dart';
import 'data.dart';
part 'user.g.dart';
#JsonSerializable()
class User {
User({
required this.data,
});
Data data;
factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
Map<String, dynamic> toJson() => _$UserToJson(this);
}
this is user_info.dart
import 'package:json_annotation/json_annotation.dart';
part 'user_info.g.dart';
#JsonSerializable()
class UserInfo {
String name;
String job;
String? id;
String? createdAt;
String? updatedAt;
UserInfo({
required this.name,
required this.job,
this.id,
this.createdAt,
this.updatedAt,
});
factory UserInfo.fromJson(Map<String, dynamic> json) =>
_$UserInfoFromJson(json);
Map<String, dynamic> toJson() => _$UserInfoToJson(this);
}
this is data.dart
import 'package:json_annotation/json_annotation.dart';
part 'data.g.dart';
#JsonSerializable()
class Data {
Data({
required this.id,
required this.email,
required this.firstName,
required this.lastName,
required this.avatar,
});
int id;
String email;
#JsonKey(name: 'first_name')
String firstName;
#JsonKey(name: 'last_name')
String lastName;
String avatar;
factory Data.fromJson(Map<String, dynamic> json) => _$DataFromJson(json);
Map<String, dynamic> toJson() => _$DataToJson(this);
}
this is the github link https://github.com/sbis04/dio_networking
Using Model to format data, is depend on developer strategy.For example here they want to categorize the user data in to different for. They have User which have a property of data that provide with other model class which is some user informations. They have other model class called UserInfo which hold other user information than Data class.
Related
How to Solve Error: Non-nullable instance field 'id' must be initialized.
Try adding an initializer expression, or add a field initializer in this constructor, or mark it 'late'.
class CategoryModel {
int id;
String name;
CategoryModel({
required this.id,
required this.name,
});
CategoryModel.fromJson(Map<String, dynamic> json) {
id = json['id'];
name = json['name'];
}
If your field (id, name e.g.) CANNOT be null at anytime, add required keyword.
class CategoryModel {
final int id;
final String name;
CategoryModel({
required this.id,
required this.name,
});
CategoryModel.fromJson(Map<String, dynamic> json) {
id = json['id'];
name = json['name'];
}
If your field CAN be null, make it nullable by adding ? and it doesn't need to be required field.
class CategoryModel {
final int? id;
final String? name;
CategoryModel({
this.id,
this.name,
});
CategoryModel.fromJson(Map<String, dynamic> json) {
id = json['id'];
name = json['name'];
}
Hello I explain my problem I have a list of patients with their coordinates all that is registered in a class by clicking on one of the patient a life opens with the information of the patient in question.
I want to add the possibility to add notes (an unlimited number of notes per patient) so I need another list but I do not know how to link the two
I would like that when I click on a patient it loads the information of this patient from the list of notes
Here is the list of my patients
class Patients {
final String name;
final String firstname;
final String dateofbirth;
final String email;
final String numero;
final String id;
final DateTime date;
Patients(
{required this.name,
required this.firstname,
required this.dateofbirth,
required this.email,
required this.numero,
required this.id,
required this.date});
}
Here is the list of patient notes
class ListNote {
final String? title;
final String? note;
final String? conclusion;
ListNote({
this.title,
this.note,
this.conclusion,
});
}
My patient screen list
In the red frame is the information of my first list of patients.
In the blue boxes you will find the notes linked to the patient
Patient page with more info
Thank you for your help
What I got from your point is you want to add List of Notes in Patient class. If that's the case you can simply add that in Patient class like this:
class Patients {
final String name;
final List<ListNote> listOfNotes;
final String firstname;
final String dateofbirth;
final String email;
final String numero;
final String id;
final DateTime date;
Patients({
required this.name,
required this.listOfNotes,
required this.firstname,
required this.dateofbirth,
required this.email,
required this.numero,
required this.id,
required this.date
});
}
class ListNote {
final String? title;
final String? note;
final String? conclusion;
ListNote({
this.title,
this.note,
this.conclusion,
});
}
I am trying to use a single model for both storing the data locally using Isar, and also for using with Retrofit for REST API requests.
But Isar requires all the Linked classes to be defined with the datatype IsarLink<MyClassName> where JsonSerializable requires them with MyClassName as the datatype.
#Collection()
#JsonSerializable()
class UserGroup {
#JsonKey(ignore: true)
Id localId = Isar.autoIncrement; // you can also use id = null to auto increment
#ignore
#JsonKey(name: "_id")
String? id;
String name;
String description;
#ignore
Domain? domain;
#ignore
UserGroupPermissions permissions;
#ignore
Organization? organization;
#JsonKey(ignore: true)
IsarLink<Organization?> organization = IsarLink<Organization?>();
UserGroup({
this.id,
required this.name,
required this.description,
this.domain,
required this.permissions,
this.organization,
});
factory UserGroup.fromJson(Map<String, dynamic> json) => _$UserGroupFromJson(json);
Map<String, dynamic> toJson() => _$UserGroupToJson(this);
}
Other than defining two variables like IsarLink<MyClassName> localOrganization with #JsonKey(ignore: true) for Isar and MyClassName with #ignore for JsonSerializable is there any other way to use a single variable for working with both of them?
Having to define two variables almost everywhere to support both of the generators makes the code look really messy
#freezed
class ABCModel with _$ABCModel {
factory ABCModel({
#JsonKey(name: "id") #Default('') String id,
#JsonKey(name: "name") #Default('') String name,
}) = _ABCModel;
factory ABCModel.fromJson(Map<String, dynamic> json) => _$ABCModelFromJson(json);
}
#freezed
class EFGModel with _$EFGModel {
factory EFGModel({
#JsonKey(name: "abc") #Default(ABCModel()) ABCModel abc, //empty ABCModel
}) = _EFGModel;
factory EFGModel.fromJson(Map<String, dynamic> json) => _$EFGModelFromJson(json);
}
If EFGModel get an empty or null abc json value, what is the suitable value to put on #Default() freezed annotation, #Default(ABCModel()) is not correct
Note the Player.blank() constructor below. Like this...
#freezed
class Player with _$Player {
Player._();
factory Player({
#Default('') String handle,
#Default('') String realname,
Contact? contactinfo,
#Default(false) bool emailverified,
#Default(false) bool showemail,
#Default(false) bool showphone,
#Default('') String avatarurl,
DateTime? datejoined,
#Default(0) int transactions,
DateTime? datelasttransaction,
DateTime? datelastlogin,
#Default([]) List<String> tags,
#Default([]) List<String> leagues,
#Default([]) List<String> events,
#Default(0) int views,
#Default(0) int likes,
#Default(0) int loginfails,
#JsonKey(ignore: true) #Default('') String password,
#JsonKey(ignore: true) #Default('') String confirm,
required Meta meta,
}) = _Player;
factory Player.blank() => Player(contactinfo: Contact(), meta: Meta());
factory Player.fromJson(Map<String, dynamic> json) => _$PlayerFromJson(json);
Suppose there are two models User and City
#JsonSerializable()
class User {
int id;
String name;
City? city;
List<Map<String, City>>? listMapCity;
}
#JsonSerializable()
class City {
int id;
String name;
}
Now suppose during API call, we've got a user model but in the city object model, we only get id not name. Something like this
{
"id": 5,
"name": "Matthew",
"city": {
"id": 12
}
}
But due to the default nature of json_serializable and json_annotation.
This JSON is not mapped to the User model, during mapping, it throws the exception.
type Null is not a subtype of type String. (because here name key is missing in city object)
But as we already declared in the User object that City is optional, I wanted that it should parse the User JSON with city and listMapCity to be null.
Any help or solution would be really appreciated, Thank you
You need to set the includeIfNull flag to false to have the autogenerated code handle nulls correctly.
#JsonSerializable(includeIfNull: false)
The property should be declared with a ? as per your example.
You need to have a default constructor on your JsonSerializable User class. Then, if name should be nullable, declare it with a nullable String? name;
Here's the updated User class.
import 'package:json_annotation/json_annotation.dart';
part 'user.g.dart';
#JsonSerializable()
class User {
int id;
String name;
City? city;
List<Map<String, City>>? listMapCity;
User({required this.id, required this.name, this.city, this.listMapCity});
factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
Map<String, dynamic> toJson() => _$UserToJson(this);
}
#JsonSerializable()
class City {
int id;
String name;
City({required this.id, required this.name});
}