Flutter Login through API

Issue

After Successful Login, I am getting User_id and User_type
I want to access the User_id and User_type.
By Using User_type,I want to log in the supervisor or Promoter

  Future postMethod() async {
    var api = Uri.parse("https://demo.likemyfiles.com/DS/api/auth/otp");
    Map mapeddate ={
      'phone':_phone.text,
      'otp':_otp.text,
    };
    //7404745159
    //8059482819
      final response = await http.post(api, body: mapeddate);
      print(response.body);

Output Will be:-

 {"error":false,"login":"success","user_id":"3","user_type":"1"}

Now I want to Access this user_type to login into the Supervisor and if the User_type:-2, I want to log in to the Promoter’s Window.

Also, I want to store User_id into another variable to Show it on the screen.

enter image description here

Solution

You can decode response.body to get user-type. Then apply if-else logic.

  var res = json.decode(response.body);
   if(res['user_type'] == "1"){
      // supervisor logic
   }else{
     // promotor window logic
   }

Also you get can user_id by using res['user_id'].

Answered By – John Joe

Answer Checked By – Mildred Charles (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.