Search

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

dio: ^5.4.0
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 ์ฃผ์š” ๋ฉ”์†Œ๋“œ

๋ฉ”์†Œ๋“œ

์ฃผ์š” ๋ฉ”์†Œ๋“œ
์„ค๋ช…
dio.get(url, {options, cancelToken, onReceiveProgress})
GET ์š”์ฒญ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
dio.post(url, data, {options, cancelToken, onSendProgress, onReceiveProgress})
POST ์š”์ฒญ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
dio.put(url, data, {options, cancelToken, onSendProgress, onReceiveProgress})
PUT ์š”์ฒญ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
dio.delete(url, {data, options, cancelToken})
DELETE ์š”์ฒญ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

๋ฉ”์†Œ๋“œ ์ธ์ž(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
๋ณต์‚ฌ