i have three types of user in my application (nurse,substitute, patient), every type will have access to specific home page. every type of user have his homepage and his specific features. so i want to implement some logic in my code to resolve this problem, i used shared_pereferences but it didn't work . how to do this in flutter? To navigate to different home pages for different users?
What I have tried:
this is my login button:
Align(
alignment: AlignmentDirectional.bottomEnd,
child: GestureDetector(
onTap: () async {
SharedPreferences prefs = await SharedPreferences.getInstance();
Map data = {
"email": _controllermail.text,
"password": _controllerpassword.text
};
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => HomePage()));
await AuthRepository().login(data);
},
child: const Image(
image: AssetImage('images/fleche.png'),
height: 70.0,
width: 70.0,
),
),
),
and this is my user model :
class User {
int id = 0;
String? type;
String? firstName;
String? lastName;
String? email;
String? password;
String? phone;
DateTime? birthDate;
String? tourSector;
String? address;
String? adeliNumber;
String? presentation;
User({
required this.id,
this.type,
this.firstName,
this.lastName,
this.email,
this.password,
this.phone,
this.birthDate,
this.tourSector,
this.address,
this.adeliNumber,
this.presentation,
});
factory User.fromJson(Map<String, dynamic> jsonResponse) {
return User(
id : jsonResponse['id'] ?? 0,
type: jsonResponse["type"] ?? "",
firstName: jsonResponse["first_name"] ?? "",
lastName: jsonResponse["last_name"] ?? "",
email: jsonResponse["email"] ?? "",
password: jsonResponse["password"] ?? "",
phone: jsonResponse["phone"] ?? "",
birthDate: jsonResponse["birth_date"] ?? DateTime.now(),
tourSector: jsonResponse["tour_sector"] ?? "",
address: jsonResponse["address"] ?? "",
adeliNumber: jsonResponse["adeli_number"] ?? "",
presentation:jsonResponse["presentation"] ?? "",
);
}
get apiToken => null;
Map<String, dynamic> toJson(User _user) {
Map<String, dynamic> nestedMap = Map();
if (_user.firstName != null && _user.firstName!.isNotEmpty) {
nestedMap["first_name"] = _user.firstName;
}
if (_user.lastName != null && _user.lastName!.isNotEmpty) {
nestedMap["last_name"] = _user.lastName;
}
if (_user.type != null && _user.type!.isNotEmpty) {
nestedMap["type"] = _user.type;
}
if (_user.phone != null && _user.phone!.isNotEmpty) {
nestedMap["phone"] = _user.phone;
}
if (_user.email != null && _user.email!.isNotEmpty) {
nestedMap["email"] = _user.email;
}
if (_user.password != null && _user.password!.isNotEmpty) {
nestedMap["password"] = _user.password;
}
if (_user.tourSector != null && _user.tourSector!.isNotEmpty) {
nestedMap["tour_Sector"] = _user.tourSector;
}
if (_user.address != null && _user.address!.isNotEmpty) {
nestedMap["address"] = _user.address;
}
if (_user.adeliNumber != null && _user.adeliNumber!.isNotEmpty) {
nestedMap["adeli_number"] = _user.adeliNumber;
}
if (_user.presentation != null && _user.presentation!.isNotEmpty) {
nestedMap["presentation"] = _user.presentation;
}
if (_user.birthDate != null && _user.birthDate!.toString().isNotEmpty) {
nestedMap["birth_date"] = _user.birthDate;
}
Map<String, dynamic> map = nestedMap;
return map;
}
}