공부하는 키보드르륵/Flutter

[날씨 App 만들기-2] http package / Json Parse

키보드르륵 2022. 10. 26. 23:34
반응형
http package

인터넷에서 데이터를 가지고 오기 위한 package로 api를 호출하여 데이터를 받아올 때 사용한다.

(https://pub.dev/packages/http)

 

http | Dart Package

A composable, multi-platform, Future-based API for HTTP requests.

pub.dev

 

http 사용법

1. pubspec.yaml 파일에 http dependencies 추가(https://pub.dev/packages/http/install)

2. 네트워크 연결을 위한 permission 추가 - 안드로이드 경우

    2.1 android > app > src > main > AndroidManifest.xml  파일에 <user-permission> 추가

3. http 사용을 위한 import 

4. get메소드를 사용하여 api 호출

 

http 호출로 받은 response

http로 호출로 response에는 아래와 같이 많은 정보를 포함하고 있다. 개발자가 필요한 값을 찾아 사용이 가능하다.

참고 : https://pub.dev/documentation/http/latest/http/Response-class.html

 

Response class - http library - Dart API

An HTTP response where the entire response body is known in advance. Inheritance Constructors Response(String body, int statusCode, {BaseRequest? request, bool isRedirect = false, bool persistentConnection = true, String? reasonPhrase}) Creates a new HTTP

pub.dev

 

dart:convert를 이용한 json data parse

1. json Data를 convert 하기 위하여 dart convert를 import 한다.

2. jsonDecode를 이용하여 response 받은 body의 정보를 json 형태로 변경

 

3. jsonDecode한 데이터에서 원하는 정보 추출하기

 

 

이번 내용도 코딩셰프 님의 강좌를 보고 직접 실습한 내용을 작성하였습니다. 

감사합니다.

 

출처 및 참고 : https://youtu.be/ccq1yCmNzdk - 플러터(Flutter) 조금 매운맛🌶️ 강좌 14 | 날씨 앱(weather app) 만들기 2

반응형