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
Basically, you tried to assign a
String
to a property that accepts only anint
, example of what you did:so you need to go from that
String
and convert it to anint
, you can useint.parse(/*String*/)
:that’s what you need to do in your case.
Note: you can do the reverse and convert an
int
toString
with thetoString()
method:Just write number.toString() it will convert it from string to int.