Skip to content

Instantly share code, notes, and snippets.

@arnold-parge
Created June 14, 2020 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arnold-parge/2355d57fe2f1cfac5ec52cafeba20a31 to your computer and use it in GitHub Desktop.
Save arnold-parge/2355d57fe2f1cfac5ec52cafeba20a31 to your computer and use it in GitHub Desktop.
User model for registering in hive database
import 'dart:convert';
import 'package:best_db_poc/utils/app_keys.dart';
import 'package:hive/hive.dart';
part 'user_model.g.dart';
@HiveType(typeId: 1)
class UserModel {
@HiveField(0)
String gender;
@HiveField(1)
_Name name;
@HiveField(2)
_Location location;
@HiveField(3)
String email;
@HiveField(4)
_Login login;
@HiveField(5)
_Dob dob;
@HiveField(6)
_Dob registered;
@HiveField(7)
String phone;
@HiveField(8)
String cell;
@HiveField(9)
_Id id;
@HiveField(10)
_Picture picture;
@HiveField(11)
String nat;
static String get tableName => 'user';
String get fullName => '${name.title}. ${name.first} ${name.last}';
UserModel({
this.gender,
this.name,
this.location,
this.email,
this.login,
this.dob,
this.registered,
this.phone,
this.cell,
this.id,
this.picture,
this.nat,
});
factory UserModel.fromJson(String str) => UserModel.fromMap(json.decode(str));
String toJson() => json.encode(toMap());
factory UserModel.fromMap(Map<String, dynamic> jsonData) => UserModel(
gender: jsonData[AppKeys.gender],
name: _Name.fromMap(jsonData[AppKeys.name]),
location: _Location.fromMap(jsonData[AppKeys.location]),
email: jsonData[AppKeys.email],
login: _Login.fromMap(jsonData[AppKeys.login]),
dob: _Dob.fromMap(jsonData[AppKeys.dob]),
registered: _Dob.fromMap(jsonData[AppKeys.registered]),
phone: jsonData[AppKeys.phone],
cell: jsonData[AppKeys.cell],
id: _Id.fromMap(jsonData[AppKeys.id]),
picture: _Picture.fromMap(jsonData[AppKeys.picture]),
nat: jsonData[AppKeys.nat],
);
Map<String, dynamic> toMap() => {
AppKeys.gender: gender,
AppKeys.name: name.toMap(),
AppKeys.location: location.toMap(),
AppKeys.email: email,
AppKeys.login: login.toMap(),
AppKeys.dob: dob.toMap(),
AppKeys.registered: registered.toMap(),
AppKeys.phone: phone,
AppKeys.cell: cell,
AppKeys.id: id.toMap(),
AppKeys.picture: picture.toMap(),
AppKeys.nat: nat,
};
}
@HiveType(typeId: 2, adapterName: 'UserDobAdapter')
class _Dob {
@HiveField(0)
DateTime date;
@HiveField(1)
int age;
_Dob({
this.date,
this.age,
});
factory _Dob.fromJson(String str) => _Dob.fromMap(json.decode(str));
String toJson() => json.encode(toMap());
factory _Dob.fromMap(Map<String, dynamic> jsonData) => _Dob(
date: DateTime.parse(jsonData[AppKeys.date]),
age: jsonData[AppKeys.age],
);
Map<String, dynamic> toMap() => {
AppKeys.date: date.toIso8601String(),
AppKeys.age: age,
};
}
@HiveType(typeId: 3, adapterName: 'UserIdAdapter')
class _Id {
@HiveField(0)
String name;
@HiveField(1)
String value;
_Id({
this.name,
this.value,
});
factory _Id.fromJson(String str) => _Id.fromMap(json.decode(str));
String toJson() => json.encode(toMap());
factory _Id.fromMap(Map<String, dynamic> jsonData) => _Id(
name: jsonData[AppKeys.name],
value: jsonData[AppKeys.value],
);
Map<String, dynamic> toMap() => {
AppKeys.name: name,
AppKeys.value: value,
};
}
@HiveType(typeId: 4, adapterName: 'UserLocationAdapter')
class _Location {
@HiveField(0)
_Street street;
@HiveField(1)
String city;
@HiveField(2)
String state;
@HiveField(3)
String country;
@HiveField(4)
String postcode;
@HiveField(5)
_Coordinates coordinates;
@HiveField(6)
_Timezone timezone;
_Location({
this.street,
this.city,
this.state,
this.country,
this.postcode,
this.coordinates,
this.timezone,
});
factory _Location.fromJson(String str) => _Location.fromMap(json.decode(str));
String toJson() => json.encode(toMap());
factory _Location.fromMap(Map<String, dynamic> jsonData) => _Location(
street: _Street.fromMap(jsonData[AppKeys.street]),
city: jsonData[AppKeys.city],
state: jsonData[AppKeys.state],
country: jsonData[AppKeys.country],
postcode: jsonData[AppKeys.postcode].toString(),
coordinates: _Coordinates.fromMap(jsonData[AppKeys.coordinates]),
timezone: _Timezone.fromMap(jsonData[AppKeys.timezone]),
);
Map<String, dynamic> toMap() => {
AppKeys.street: street.toMap(),
AppKeys.city: city,
AppKeys.state: state,
AppKeys.country: country,
AppKeys.postcode: postcode,
AppKeys.coordinates: coordinates.toMap(),
AppKeys.timezone: timezone.toMap(),
};
}
@HiveType(typeId: 5, adapterName: 'UserCoordinatesAdapter')
class _Coordinates {
@HiveField(0)
String latitude;
@HiveField(1)
String longitude;
_Coordinates({
this.latitude,
this.longitude,
});
factory _Coordinates.fromJson(String str) =>
_Coordinates.fromMap(json.decode(str));
String toJson() => json.encode(toMap());
factory _Coordinates.fromMap(Map<String, dynamic> jsonData) => _Coordinates(
latitude: jsonData[AppKeys.latitude],
longitude: jsonData[AppKeys.longitude],
);
Map<String, dynamic> toMap() => {
AppKeys.latitude: latitude,
AppKeys.longitude: longitude,
};
}
@HiveType(typeId: 6, adapterName: 'UserStreetAdapter')
class _Street {
@HiveField(0)
int number;
@HiveField(1)
String name;
_Street({
this.number,
this.name,
});
factory _Street.fromJson(String str) => _Street.fromMap(json.decode(str));
String toJson() => json.encode(toMap());
factory _Street.fromMap(Map<String, dynamic> jsonData) => _Street(
number: jsonData[AppKeys.number],
name: jsonData[AppKeys.name],
);
Map<String, dynamic> toMap() => {
AppKeys.number: number,
AppKeys.name: name,
};
}
@HiveType(typeId: 7, adapterName: 'UserTimezoneAdapter')
class _Timezone {
@HiveField(0)
String offset;
@HiveField(1)
String description;
_Timezone({
this.offset,
this.description,
});
factory _Timezone.fromJson(String str) => _Timezone.fromMap(json.decode(str));
String toJson() => json.encode(toMap());
factory _Timezone.fromMap(Map<String, dynamic> jsonData) => _Timezone(
offset: jsonData[AppKeys.offset],
description: jsonData[AppKeys.description],
);
Map<String, dynamic> toMap() => {
AppKeys.offset: offset,
AppKeys.description: description,
};
}
@HiveType(typeId: 8, adapterName: 'UserLoginAdapter')
class _Login {
@HiveField(0)
String uuid;
@HiveField(1)
String username;
@HiveField(2)
String password;
@HiveField(3)
String salt;
@HiveField(4)
String md5;
@HiveField(5)
String sha1;
@HiveField(6)
String sha256;
_Login({
this.uuid,
this.username,
this.password,
this.salt,
this.md5,
this.sha1,
this.sha256,
});
factory _Login.fromJson(String str) => _Login.fromMap(json.decode(str));
String toJson() => json.encode(toMap());
factory _Login.fromMap(Map<String, dynamic> jsonData) => _Login(
uuid: jsonData[AppKeys.uuid],
username: jsonData[AppKeys.username],
password: jsonData[AppKeys.password],
salt: jsonData[AppKeys.salt],
md5: jsonData[AppKeys.md5],
sha1: jsonData[AppKeys.sha1],
sha256: jsonData[AppKeys.sha256],
);
Map<String, dynamic> toMap() => {
AppKeys.uuid: uuid,
AppKeys.username: username,
AppKeys.password: password,
AppKeys.salt: salt,
AppKeys.md5: md5,
AppKeys.sha1: sha1,
AppKeys.sha256: sha256,
};
}
@HiveType(typeId: 9, adapterName: 'UserNameAdapter')
class _Name {
@HiveField(0)
String title;
@HiveField(1)
String first;
@HiveField(2)
String last;
_Name({
this.title,
this.first,
this.last,
});
factory _Name.fromJson(String str) => _Name.fromMap(json.decode(str));
String toJson() => json.encode(toMap());
factory _Name.fromMap(Map<String, dynamic> jsonData) => _Name(
title: jsonData[AppKeys.title],
first: jsonData[AppKeys.first],
last: jsonData[AppKeys.last],
);
Map<String, dynamic> toMap() => {
AppKeys.title: title,
AppKeys.first: first,
AppKeys.last: last,
};
}
@HiveType(typeId: 10, adapterName: 'UserPictureAdapter')
class _Picture {
@HiveField(0)
String large;
@HiveField(1)
String medium;
@HiveField(2)
String thumbnail;
_Picture({
this.large,
this.medium,
this.thumbnail,
});
factory _Picture.fromJson(String str) => _Picture.fromMap(json.decode(str));
String toJson() => json.encode(toMap());
factory _Picture.fromMap(Map<String, dynamic> jsonData) => _Picture(
large: jsonData[AppKeys.large],
medium: jsonData[AppKeys.medium],
thumbnail: jsonData[AppKeys.thumbnail],
);
Map<String, dynamic> toMap() => {
AppKeys.large: large,
AppKeys.medium: medium,
AppKeys.thumbnail: thumbnail,
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment