skip to Main Content

I am using Node API with RENDER hosting, while I host the backend it works and when I try to connect the front end and send data I get an exception named Unhandled Exception: type ‘String’ is not a subtype of type ‘int’ of ‘index’ help me, please

note: password is in string and number is an int data type

RoundedButton(
  colour: Colors.lightBlueAccent,
  title: 'Login',
  onPressed: () {
    AuthService().login(number, password).then((val) {
      if (val.data['success']) {
        var token = val.data['token'];
        Fluttertoast.showToast(
            msg: 'SUCCESS',
            toastLength: Toast.LENGTH_SHORT,
            gravity: ToastGravity.BOTTOM,
            timeInSecForIosWeb: 1,
            backgroundColor: Colors.green,
            textColor: Colors.white,
            fontSize: 16.0);
      }
    });
    print('phone: $number && password:$password');
  },
),
class AuthService {
  Dio dio = Dio();
  login(phone, password) async {
    try {
      return await dio.post('https://parkit-odj8.onrender.com/signin',
          data: {"phone": phone, "password": password},
          options: Options(contentType: Headers.formUrlEncodedContentType));
    } on DioError catch (e) {
      Fluttertoast.showToast(
          msg: e.response?.data['msg'],
          toastLength: Toast.LENGTH_SHORT,
          gravity: ToastGravity.BOTTOM,
          backgroundColor: Colors.red,
          textColor: Colors.white,
          fontSize: 16.0);
    }
  }
}

This is my code I tried looking up everything and tried changing my data types but still no use

2

Answers


  1. Basically, you tried to assign a String to a property that accepts only an int, example of what you did:

    String a = "100";
    int b = a;  // throws an error
    

    so you need to go from that String and convert it to an int, you can use int.parse(/*String*/):

    String a = "100";
    int b = int.parse(a);  // 100
    

    that’s what you need to do in your case.

    Note: you can do the reverse and convert an int to String with the toString() method:

    int b = 100;
    String a = b.toString();  // "100"
    
    Login or Signup to reply.
  2. Just write number.toString() it will convert it from string to int.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search