I am encountering an intermittent FormatException with the message “Unexpected end of input” when fetching data from an API using the Dio library in a Flutter application. This exception does not occur consistently; sometimes I get the correct data, and other times this error occurs.
DioException (DioException [unknown]: null
Error: FormatException: Unexpected end of input (at character 3247)
...2024-05-06T15:00:41.000000Z","updated_at":"2024-05-07T08:51:56.000000Z"}}}]
here is my repository
Future<ApiResult<AppointmentsResponse>> getAppointments() async {
final dio = Dio();
dio.interceptors.add(LogInterceptor(responseBody: true));
dio.interceptors.add(LogInterceptor(requestBody: true));
try {
final prefs = await SharedPreferences.getInstance();
final String? stored_token = await prefs.getString('token');
final int? tech_id = await prefs.getInt('tech_id');
final dio = DioFactory.getDio();
final response = await dio.get(
'$baseurl/api/appointments/tech/$tech_id/all',
options: Options(
headers: {'Authorization': 'Bearer $stored_token'},
));
debugPrint("Raw API Response: $response");
debugPrint('Response: ${response.statusCode}');
debugPrint("response11: ${response.data}");
// Process your response data here
Map<String, dynamic> responseData = response.data is Map<String, dynamic>
? response.data
: jsonDecode(response.data);
return ApiResult.success(AppointmentsResponse.fromJson(responseData));
} catch (e, stacktrace) {
logger.e("Error: $e, StackTrace: $stacktrace");
return ApiResult.failure(e.toString());
}
}
I also have this in my bloc provider
BlocProvider<AppointmentCubit>(
create: (BuildContext context) => AppointmentCubit(AppointmentRepo(
apiService: ApiService(Dio(BaseOptions(
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
},
followRedirects: false,
// // will not throw errors
validateStatus: (status) => true,
))))),
),