Home > Mobile >  API connection doesn't work properly flutter
API connection doesn't work properly flutter

Time:04-06

when I'm entering the correct email and password but I'm getting "Something went wrong..." Error snack bar. I want to display logged in successfully. postman request is working properly. struggling to find the error. how to resolve this. I attached enter image description here

Future LoginData() async {
    setState(() {
      isLoading = true;
      typing = false;
    });
    try {
      var response = await Dio().post(BASE_API   'user/login',
          data: {"email": email, "password": password});

      if (response.data["message"] == "logged in successfully") {
          setState(() {
            isLoading = false;
          });

        Get.snackbar("success", "logged in successfully");
          Get.to(BottomNavigation());
      }
   
     else {
        setState(() {
          isLoading = false;
          typing = true;
        });
        Get.snackbar(
          "error",
          "No User Found",
          // backgroundColor: heartRed.withOpacity(0.8),
          // colorText: textWhite,
        );
      }
      print("res: $response");
      setState(() {
        isLoading = false;
        typing = true;
      });
    } catch (e) {
      setState(() {
        isLoading = false;
        typing = true;
      });
      Get.snackbar("Error", "Something went wrong.Please contact admin",
          backgroundColor: buttontext .withOpacity(0.5),
          borderWidth: 1,
          borderColor: Colors.grey,
          colorText: Colors.white,
          icon: Icon(
            Icons.error_outline_outlined,
            color: Colors.red,
            size: 30,
          ));
      print(e);
    }
  }




 isLoading
              ? SpinKitDualRing(
                  color: textWhite,
                  size: 40,
                )
              : GestureDetector(
                  child: MainButton("Login"),
                  onTap: () async {
                    FocusManager.instance.primaryFocus?.unfocus();
                    if (_formKey.currentState!.validate()) {
                      _formKey.currentState!.save();
                       await LoginData();
                     // Get.to(BottomNavigation());
                    }
                  },
                ),

CodePudding user response:

Try correcting var response = await Dio().post(BASE_API 'user/login', data: {"email": email, "password": password}); line.Do like below

var response = await Dio().post(BASE_API   'user/login',
      data: {"username": email, "password": password});
//...
if(response.statusCode==200){...}

CodePudding user response:

Try your ip address instead of localhost; You can get your id adress by typing ipconfig in cmd.

enter image description here

Put your ip adress like this;

http://your-ip-adress/user/login

  • Related