i have problem encontering error in my flutter apps, i wanna fetch some data to list using dio get. in flutter that wrap on getX Controller.
here the code that has a problem, i have tried many way like
using DioExceptionType to catch the spesific problem, and using DioException.statuscode. the error still cannot be catches.
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 from 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):
oh if you wanna know the dio get method here, take a look
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;
}
}
can somebody help, me to catch the ‘e’
it mean alot.
and thank by the way, sorry for inappropriate english, english is my 2nd language