I want to fetch some data to list using dio get. In Flutter that wraps a getX controller.
Here is the code that has a problem. I have tried many ways, like
using DioExceptionType
to catch the specific problem, and using DioException.statuscode
. However, the error still cannot be caught.
class JemaatController extends GetxController {
RxList<JemaatJSON> jemaat = RxList();
RxBool isListViewScrollToTheDown = false.obs;
RxBool isLoading = true.obs;
RxBool isInternetConnect = true.obs;
final deviceStorage = GetStorage();
var url = "${ConfigBack.apiAdress}/admin/jemaat/";
var itemController = ItemScrollController();
getJemaat() async {
var response = await DioService().getMethod(url);
isLoading.value = true;
try {
if (response.statusCode == 200) {
response.data.forEach((element) {
jemaat.add(JemaatJSON.fromJson(element));
});
}
Future.delayed(const Duration(seconds: 1), () {
isLoading.value = false;
});
} catch (e) {
isLoading.value = false;
if (e is DioException) {
if (e.response?.statusCode == 401) {
FilemonHelperFunctions.showSnackBar(
"Waktu sesi telah berakhir silahkan Re-Log");
deviceStorage.write('user_login', false);
deviceStorage.write('IsFirstTime', false);
print(deviceStorage.read('user_login'));
print(deviceStorage.read('IsFirstTime'));
deviceStorage.remove('usertoken');
deviceStorage.remove('userC');
NavigationAdmin().toMain();
if (e.type == DioExceptionType.connectionError) {
FilemonHelperFunctions.showSnackBar(
"Koneksi bermasalah, ini bukan pada perangkat anda");
}
}
}
}
}
remJemaat() async {
jemaat.clear();
}
scrollListViewDownward() {
itemController.scrollTo(
index: jemaat.length - 4,
duration: const Duration(seconds: 2),
curve: Curves.fastOutSlowIn);
isListViewScrollToTheDown.value = true;
}
/// Scroll ListView To Up
scrollListViewUpward() {
itemController.scrollTo(
index: 0,
duration: const Duration(seconds: 2),
curve: Curves.fastOutSlowIn);
isListViewScrollToTheDown.value = false;
}
@override
void onInit() {
getJemaat();
super.onInit();
}
}
and for the error that I got, this is what's shown in the debug console:
E/flutter (12149): #0 DioMixin.fetch (package:dio/src/dio_mixin.dart:509:7)
E/flutter (12149): <asynchronous suspension>
E/flutter (12149): #1 DioService.getMethod.<anonymous closure> (package:MobileGKI/data/api_config.dart:102:54)
E/flutter (12149): <asynchronous suspension>
E/flutter (12149): #2 DioService.getMethod (package:MobileGKI/data/api_config.dart:102:12)
E/flutter (12149): <asynchronous suspension>
E/flutter (12149): #3 JemaatController.getJemaat (package:MobileGKI/data/crud_state/jemaat/jemaatlisting.dart:26:20)
E/flutter (12149): <asynchronous suspension>
E/flutter (12149):
The dio get method is used here:
class DioService {
Future<dynamic> getMethod(String url) async {
dioUpers.Dio dio = dioUpers.Dio();
dioUpers.Options options =
dioUpers.Options(headers: HeadersCode().data(), method: "GET");
return await dio.get(url, options: options).then((response) {
return response;
});
}`
Don't worry about one of '}' on DioService, is not the whole script. and for the header
class HeadersCode {
final deviceStorage = GetStorage();
data() {
Map<String, dynamic> headers = {
'Authorization': 'Bearer ${deviceStorage.read("usertoken")}',
'Content-Type': 'application/json',
// Add more headers if needed
};
return headers;
}
}
How can I catch the error?
You need to put the call also in the try
block:
try {
var response = await DioService().getMethod(url);
isLoading.value = true;
if (response.statusCode == 200) {
response.data.forEach((element) {
jemaat.add(JemaatJSON.fromJson(element));
});
}
If something goes wrong catch
block will be executed.