Dio (Http client for Dart) get request is not working with interceptors
5,283
UPDATED
dio = Dio();
dio.options.baseUrl = URL_API_PROD;
dio.interceptors.add(InterceptorsWrapper(
onRequest: (Options option) async{
//my function to recovery token
await getToken().then((result) {
token = result;
});
option.headers = {
"Authorization": "Bearer $token"
};
}
));
//here i use the dio instance on constuctor and call the get verb to get the data
Future<List<Children>> getChildren() async {
Response response = await dio.get('/v1/pessoa/alunosresponsavel');
//here I map the json from response to my model(children)
return (response.data['Dados'] as List).map((child)=> Children.fromJson(child)).toList();
}
getToken.dart
import 'package:shared_preferences/shared_preferences.dart';
getToken() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
String getToken = preferences.getString("LastToken");
return getToken;
}
PS-> The dependecy shared_preferences is necessary
Author by
Admin
Updated on December 12, 2022Comments
-
Admin over 1 year
Actually i want to use dio (Http client for Dart) in my project for all http request,i checked in official doc but not able to apply.
with http Client from package:http/http.dart it's working perfect but i want to use with Dio.Can any one please check and help me,why it's not working.Thanks in advance !
import 'dart:convert'; import 'package:http/http.dart' as http; import 'package:thunder_mobile/models/login_modal.dart'; import 'package:thunder_mobile/utils/all_shared_preference_helper.dart'; import 'package:thunder_mobile/utils/http.dart'; import 'package:dio/dio.dart'; class ApiHelper { // Dio _dio = new Dio(); var url = "http://5c9db1fd3be4e30014a7d3da.mockapi.io/"; final thunderBaseUrl = 'https://{domain}/api/v1/'; var headers = {'Content-Type': 'application/json'}; String token; var sharedPref = new AllSharedPreferenceHelper(); var thunderHeaders = { 'content-type': 'application/json', 'x-requested-with': 'XMLHttpRequest', }; var thunderImageHeaders = { 'content-type': 'multipart/form-data', 'x-requested-with': 'XMLHttpRequest', }; final loginHeader = {'X-Requested-With': 'XMLHttpRequest'}; setApiHeader() { sharedPref.getLoginData().then((res) { LoginModel loginData = LoginModel.fromJson(json.decode(res)); if (loginData.accessToken != null) { thunderHeaders['authorization'] = 'Bearer ' + loginData.accessToken; token = loginData.accessToken; } }); } // --------------------http BASED (Working Successfully)-------------------------------------- Future getThunderRequest(apiUrl) async { await setApiHeader(); final http.Response response = await http.get(thunderBaseUrl + apiUrl, headers: thunderHeaders); return response; } Future postThunderRequest(apiUrl, body) async { await setApiHeader(); final response = await http.post(thunderBaseUrl + apiUrl, headers: thunderHeaders, body: json.encode(body.toJson())); return response; } // ----------------------DIO API'S(not Working)------------------------------------ Future getDioRequest(apiUrl) async { Dio dio = new Dio(); dio.interceptors .add(InterceptorsWrapper(onRequest: (RequestOptions options) async { await setApiHeader(); options.headers["token"] = thunderHeaders; return options; })); try { Response response = await Dio().get('https://{domain}/api/v1/master'); print(response); } catch (e) { print(e); } }```
-
Zeeshan Ansari over 4 yearsthanks man ! and please also share where you called get request and where you used interceptor.. thanks again for your response..
-
Abdulmalek Dery about 3 yearsI'm getting DioError [DioErrorType.RESPONSE]: XMLHttpRequest error when adding header to the dio request using this method