okay so im using dio package to authenticate users in my app. it works fine but i want to be able to show the error message in a toast message for the user and i have absolutely no idea how to do that. i tried printing the error but i get no response, please help me out im new to dio.
This is my code
class Loginservice extends ILogin {
@override
Future<UserModel?> login(
String username, String password, BuildContext context) async {
@override
SharedPreferences preferences = await SharedPreferences.getInstance();
String api = '$baseUrl/login';
final data = {"username": username, "pwd": password};
final dio = Dio();
Response response;
response = await dio.post(api, data: data);
if (response.statusCode == 200) {
final body = response.data;
var username = body['username'];
var myToken = body['token'];
preferences.setString('username', username);
preferences.setBool('isLoggedIn', true);
Navigator.push(
context, MaterialPageRoute(builder: (context) => MainScreen()));
return UserModel(
username: username,
token: body['token'],
);
} else {
print('error');
}
}
}
2
Answers
response has two factors: statusMessage and statusCode.
Why don’t you try this.
change A to B
A
B
or
As mentioned in their docs,
You can wrap the call with a try/ catch to catch them.
and DioException contain these fields,
Personally, I would use a switch statement to catch all the DioExceptionType s and handle/ return error messages accordingly.
you can find all the exception types here or you can simply go to the plugin source in your flutter app.