I’m having problems trying to consume my API, and after following up on this problem I’m stuck in the matter and I’ve tried different emulators in various versions and the problem persists.
Error:
DioError [DioErrorType.other]: HttpException: Connection closed before full header was received, uri = http://10.0.2.2:7108/Users/authenticate
Flutter doctor
Http Post
class AuthenticateRemoteApi extends AuthenticateGateway {
final AuthenticateMapper _authenticateMapper = AuthenticateMapper();
@override
Future<SesionUser> login(Authenticate user) async {
var dio = Dio();
dio.options.headers['content-Type'] = 'application/json';
String url = 'http://10.0.2.2:7108/Users/authenticate';
try {
Response response = await dio.post(url, data: authenticateModelToJson(user));
return _authenticateMapper.fromMap(jsonDecode(response.data));
} catch (e) {
throw Exception(e);
}
}
}
3
Answers
I think this could be a package bug, usually appears when the user doesn’t has internet connect. Reported in #1269 and #377 issue from the package repository. I recommend you to use another package.
I have came with this work around by creating this interceptor.
The idea of it is when encountering this random error just to retry the request.
Usage: add this interceptor [RetryOnConnectionChangeInterceptor] to your Dio client instance
In my case this error always occurs when I build app on emulator, rarely appears on real device. If you are using Android Studio, try with Pixel 6 API 33.