Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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();
 //String? isNurse = prefs.getString('nurse');
 //String? isSub = prefs.getString('substitute');
 //String? isPatient = prefs.getString('patient');
   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;
  }
}
Posted
Comments
wseng 24-Mar-22 0:10am    
why you comment out the three line ? Are you able to get the value of isNurse,isSub,isPatient?

1 solution

Conceptionally I would make a LoadingWidget where you execute the async Function to get the bool is<type>.

Then in the async Function:

Dart
if (is<type1>) Navigator.push(MaterialPageRoute...);

else if (is<type2>) Navigator.push(MaterialPageRoute...); 


etc.

If you need more details please let me now your specific problem
 
Share this answer
 
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900