Search
Duplicate

dio

dio

HTTP μš”μ²­ 처리 및 인터셉터λ₯Ό μ§€μ›ν•˜λŠ” Dart μ–Έμ–΄ 기반 라이브러리
Flutterμ—μ„œ DioλŠ” κ°•λ ₯ν•˜κ³  μœ μ—°ν•œ HTTP λ„€νŠΈμ›Œν‚Ή λΌμ΄λΈŒλŸ¬λ¦¬μž…λ‹ˆλ‹€. Dioλ₯Ό μ‚¬μš©ν•˜λ©΄ HTTP μš”μ²­μ„ μ‰½κ²Œ λ§Œλ“€κ³  μˆ˜μ •ν•  수 μžˆμŠ΅λ‹ˆλ‹€. 이 λΌμ΄λΈŒλŸ¬λ¦¬λŠ” GET, POST, PUT, DELETE λ“±μ˜ λͺ¨λ“  HTTP λ©”μ†Œλ“œλ₯Ό μ§€μ›ν•˜λ©°, μΏ ν‚€ 관리, 파일 μ—…λ‘œλ“œ 및 λ‹€μš΄λ‘œλ“œ, μš”μ²­ μ·¨μ†Œ, μš”μ²­ νƒ€μž„μ•„μ›ƒ λ“±μ˜ κ³ κΈ‰ κΈ°λŠ₯도 μ œκ³΅ν•©λ‹ˆλ‹€.

μ£Όμš”κΈ°λŠ₯

β€’
HTTP μš”μ²­ 처리
β€’
인터셉터
β€’
μΏ ν‚€ 관리
β€’
파일 μ—…λ‘œλ“œ 및 λ‹€μš΄λ‘œλ“œ
β€’
μš”μ²­ μ·¨μ†Œ 및 νƒ€μž„μ•„μ›ƒ μ„€μ •

μ˜μ‘΄μ„±

Dart install λͺ…λ Ήμ–΄

dart pub add dio
Dart
볡사

Flutter install λͺ…λ Ήμ–΄

flutter pub add dio
Dart
볡사

pubspec.yaml

dependencies: dio: ^5.8.0+1
Dart
볡사

import

import 'package:dio/dio.dart';
Dart
볡사

κΈ°λ³Έ μ˜ˆμ‹œμ½”λ“œ

import 'package:dio/dio.dart'; void fetchData() async { Dio dio = Dio(); try { Response response = await dio.get('https://api.example.com/data'); print(response.data); } catch (error) { print('Error: $error'); } }
Dart
볡사

dio μ£Όμš” λ©”μ†Œλ“œ

λ©”μ†Œλ“œ

κΈ°λŠ₯
μ£Όμš” λ©”μ†Œλ“œ
GET
dio.get(url, {options, cancelToken, onReceiveProgress})
POST
dio.post(url, data, {options, cancelToken, onSendProgress, onReceiveProgress})
PUT
dio.put(url, data, {options, cancelToken, onSendProgress, onReceiveProgress})
DELETE
dio.delete(url, {data, options, cancelToken})

λ©”μ†Œλ“œ 인자(parameters)

속성
μ„€λͺ…
data
POST 및 PUT μš”μ²­μ—μ„œ 전솑할 데이터λ₯Ό μ§€μ •
options
μš”μ²­μ— λŒ€ν•œ λ‹€μ–‘ν•œ 섀정을 ν¬ν•¨ν•˜λŠ” μ˜΅μ…˜ 객체
cancelToken
μš”μ²­μ„ μ·¨μ†Œν•  λ•Œ μ‚¬μš©λ˜λŠ” 토큰
onSendProgress
μ—…λ‘œλ“œ μ§„ν–‰ 상황을 μΆ”μ ν•˜λŠ” 콜백 ν•¨μˆ˜
onReceiveProgress
λ‹€μš΄λ‘œλ“œ μ§„ν–‰ 상황을 μΆ”μ ν•˜λŠ” 콜백 ν•¨μˆ˜

μš”μ²­ λ©”μ†Œλ“œ 별 μ˜ˆμ‹œμ½”λ“œ

β€’
get()
β€’
post()
β€’
put()
β€’
delete()

get()

import 'package:dio/dio.dart'; void fetchData() async { Dio dio = Dio(); try { Response response = await dio.get('https://api.example.com/data'); print(response.data); } catch (error) { print('Error: $error'); } }
Dart
볡사

post()

import 'package:dio/dio.dart'; void postData() async { Dio dio = Dio(); Map<String, dynamic> data = { 'username': 'john_doe', 'email': 'john@example.com', }; try { Response response = await dio.post('https://api.example.com/users', data: data); print(response.data); } catch (error) { print('Error: $error'); } }
Dart
볡사

put()

import 'package:dio/dio.dart'; void updateData() async { Dio dio = Dio(); Map<String, dynamic> data = { 'username': 'john_doe_updated', 'email': 'john@example.com', }; try { Response response = await dio.put('https://api.example.com/users/1', data: data); print(response.data); } catch (error) { print('Error: $error'); } }
Dart
볡사

delete()

import 'package:dio/dio.dart'; void deleteData() async { Dio dio = Dio(); try { Response response = await dio.delete('https://api.example.com/users/1'); print(response.data); } catch (error) { print('Error: $error'); } }
Dart
볡사

http vs dio

Dartμ—μ„œ HTTP μš”μ²­μ„ μˆ˜ν–‰ν•  λ•Œ http νŒ¨ν‚€μ§€μ™€ dio νŒ¨ν‚€μ§€λŠ” κ°€μž₯ 많이 μ‚¬μš©λ˜λŠ” λΌμ΄λΈŒλŸ¬λ¦¬μž…λ‹ˆλ‹€. 각각의 차이점을 ν‘œλ‘œ 정리해 λ³΄κ² μŠ΅λ‹ˆλ‹€.

http vs dio 비ꡐ ν‘œ

ν•­λͺ©
http νŒ¨ν‚€μ§€
dio νŒ¨ν‚€μ§€
μ„€μΉ˜
dart pub add http
dart pub add dio
μ‚¬μš© 방식
기본적인 HTTP μš”μ²­ 지원
κ°•λ ₯ν•œ κΈ°λŠ₯을 κ°–μΆ˜ HTTP ν΄λΌμ΄μ–ΈνŠΈ
JSON λ³€ν™˜
μˆ˜λ™μœΌλ‘œ jsonDecode() μ‚¬μš©
μžλ™μœΌλ‘œ JSON λ³€ν™˜ 지원
μš”μ²­/응닡 인터셉터
μ—†μŒ
μš”μ²­/응닡 인터셉터 지원
νƒ€μž„μ•„μ›ƒ μ„€μ •
μ—†μŒ
connectTimeout, receiveTimeout 제곡
FormData 지원
multipart 방식 μ‚¬μš©
FormData 클래슀 제곡
파일 μ—…λ‘œλ“œ
직접 multipart/form-data κ΅¬ν˜„
FormData와 onSendProgress 지원
슀트림 지원
http.StreamedRequest μ‚¬μš©
기본적으둜 μŠ€νŠΈλ¦Όμ„ 지원
μ·¨μ†Œ κΈ°λŠ₯
μ§€μ›ν•˜μ§€ μ•ŠμŒ
CancelToken을 μ‚¬μš©ν•΄ μš”μ²­ μ·¨μ†Œ κ°€λŠ₯
μ—λŸ¬ 처리
λ‹¨μˆœν•œ try-catch μ‚¬μš©
μƒμ„Έν•œ μ˜ˆμ™Έ 및 μ—λŸ¬ 핸듀링 지원

μ–΄λ–€ κ²½μš°μ— μ‚¬μš©ν•΄μ•Ό ν• κΉŒ?

β€’
http νŒ¨ν‚€μ§€
β—¦
κ°„λ‹¨ν•œ REST API 호좜만 ν•„μš”ν•œ 경우
β—¦
νŒ¨ν‚€μ§€ 크기λ₯Ό μ΅œμ†Œν™”ν•΄μ•Ό ν•˜λŠ” 경우
β€’
dio νŒ¨ν‚€μ§€
β—¦
인터셉터, νƒ€μž„μ•„μ›ƒ, μš”μ²­ μ·¨μ†Œ, 파일 μ—…λ‘œλ“œ 등이 ν•„μš”ν•œ 경우
β—¦
JSON λ³€ν™˜κ³Ό 같은 편의 κΈ°λŠ₯을 ν™œμš©ν•˜κ³  싢은 경우
httpλŠ” 가볍고 κ°„λ‹¨ν•˜μ§€λ§Œ, dioλŠ” 더 κ°•λ ₯ν•œ κΈ°λŠ₯을 μ œκ³΅ν•©λ‹ˆλ‹€. ν”„λ‘œμ νŠΈμ˜ μš”κ΅¬μ‚¬ν•­μ— 따라 μ„ νƒν•˜λ©΄ λ©λ‹ˆλ‹€!