Flutter/Package

[Flutter] dio

찌김이 2022. 8. 7. 16:28
728x90
반응형

 

http 통신을 위해 사용되는 dio 에 대해서 포스팅 합니다.

 

 

dio | Dart Package

A powerful Http client for Dart, which supports Interceptors, FormData, Request Cancellation, File Downloading, Timeout etc.

pub.dev

 

pubspec.yaml

dependencies:
  dio: ^4.0.6

 

사용

void main() async {
   final dio = Dio(); 
   
   // get
   await dio.get('path'); 
   
   // add queryParameters
   await dio.get('path', queryParameters: { 'page' : 1, "order" : "desc" });
   
   // post
   await dio.post('path'); 
   
   // add requestBody
   await dio.post('path', data : 'postData'); 
   
   // delete
   await dio.delete('path');
   
   // put
   await dio.put('path'); 
}

 

BaseOptions

  • BaseOptions 로 baseUrl, header 지정할 수 있습니다.
final baseOptions = BaseOptions(
     baseUrl: 'baseUrl',
     headers: { 'Authorization' : 'Bearer Token' },
     connectTimeout = 5000,
     receiveTimeout = 3000,
     // contentType = 'multipart/form-data' // 'Application/json'
   );
   
final dio = Dio(baseOptions);

 

로그를 남기고 싶다면 ..

 

 

pretty_dio_logger | Dart Package

Pretty Dio logger is a Dio interceptor that logs network calls in a pretty, easy to read format.

pub.dev

 

pubspec.yaml

dependencies:
  pretty_dio_logger: ^1.1.1
final dio = Dio();

final logger = PrettyDioLogger(
    requestHeader: true,
    requestBody: true,
    responseBody: true,
    responseHeader: false,
    compact: false,
  );

dio.interceptors.add(logger);
728x90
반응형

'Flutter > Package' 카테고리의 다른 글

[Flutter] image_picker  (1) 2022.08.13
[Flutter] photo_manager  (0) 2022.08.11
[Flutter] Bloc ⑤ - Bloc Test  (0) 2022.08.05
[Flutter] Bloc ④ - context.read, context.watch, context.select  (0) 2022.08.05
[Flutter] Bloc ③ - Bloc Widgets  (0) 2022.08.04