Search
🌐

http - HTTP request 라이브러리

부제
Flutter Http 요청 표준
카테고리
테크
세부 카테고리
Flutter
Combine 카테고리
최종편집일
2023/07/27 07:53
작성중
관련된 포스팅
생성 일시
2023/07/13 05:21
태그

WHAT IS IT?

HTTP 요청을 보내는 Future 기반의 라이브러리

HOW TO USE

$ flutter pub add http
Dart
복사
http 라이브러리를 프로젝트 디렉터리에 설치해준다.
import 'package:http/http.dart' as http; // 0. 라이브러리 임포트
Dart
복사
라이브러리 임포트.
Future<List<Photo>> fetch(String query) async { }
Dart
복사
네트워크 요청은 비동기적으로 일어나기 때문에, Future 타입을 리턴하는 함수로 선언해주고, async 키워드를 붙여준다.
Future<List<Photo>> fetch(String query) async { var url = Uri.https('example.com', 'whatsit/create'); // 1. }
Dart
복사
URL 객체 생성
Future<List<Photo>> fetch(String query) async { var url = Uri.https('example.com', 'whatsit/create'); // 1. var response = await http.post(url, body: {'name': 'doodle', 'color': 'blue'}); }
Dart
복사
HTTP 요청
Future<List<Photo>> fetch(String query) async { var url = Uri.https('example.com', 'whatsit/create'); var response = await http.post(url, body: {'name': 'doodle', 'color': 'blue'}); if (response.statusCode == 200) { // 1. 네트워킹 성공 여부 확인 Map<String, dynamic> jsonResponse = jsonDecode(response.body); // 2. JSON 디코딩 Iterable hits = jsonResponse['hits']; return hits.map((e) => Photo.fromJson(e)).toList(); } throw Error(); }
Dart
복사

FOR MORE