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๋ ๋ ๊ฐ๋ ฅํ ๊ธฐ๋ฅ์ ์ ๊ณตํฉ๋๋ค. ํ๋ก์ ํธ์ ์๊ตฌ์ฌํญ์ ๋ฐ๋ผ ์ ํํ๋ฉด ๋ฉ๋๋ค! 