I am new to flutter.
I am using an interceptor to catch 401 http codes the API might respond. But I don't really understand how to make it go to login page when it happens. I wanted to use Navigator, but it requires a context which I don't have.
class AuthInterceptor implements InterceptorContract {
@override
Future<RequestData> interceptRequest({RequestData data}) async {
print(data);
return data;
}
@override
Future<ResponseData> interceptResponse({ResponseData data}) async {
if (data.statusCode == 401) {
// What to do to go to login page?
}
return data;
}
}
Can someone help me with it?
You can copy paste run full code below
I use official weather example to simulate this case, because
OPEN_WEATHER_API_KEY
is invalid will return401
You can pass context to
AuthInterceptor
when initcode snippet
working demo
full code