[flutter]인터넷 서버에 연결하는 패키지(http)
https://pub.dev/packages/http/versions
http package - All Versions
Pub is the package manager for the Dart programming language, containing reusable libraries & packages for Flutter and general Dart programs.
pub.dev
인터넷이나 서버에 연결할 때 사용하는 패키지입니다.
Null Safety를 적용하고 싶으시면 0.13.6을 사용하심면 되고
최신을 사용하고 싶으시면 현재 버전을 사용하시면 됩니다.
pubspec.yaml 파일의 dependency에 추가합니다.
http: ^0.13.6
저는 인터넷 강의를 듣는 중이라서 최신 버전이 아니라 Null-Safety 버전을 선택하였습니다.
인터넷 접속을 위한 허락(Permission)을 얻기 위한 구문을 추가시킵니다.
android-> app -> src -> main에 있는
androidManifest.xml파일에 아래 문장을 추가시켜줍니다.
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Required to fetch data from the internet. -->
<uses-permission android:name="android.permission.INTERNET" />
이 부분에 대한 설명은 아래 사이트를 참고하시기 바랍니다.
https://docs.flutter.dev/cookbook/networking/fetch-data
Fetch data from the internet
How to fetch data over the internet using the http package.
docs.flutter.dev
또한 인터넷을 통한 데이터 저장이나 데이터 가져오기 등의 예제도 위 사이트를 참조하시기 바랍니다.
http를 사용하는 프로그램에 선언하고
import 'package:http/http.dart' as http;
실제 데이터를 가져오는 곳은
void fetchData() async{
http.Response response = await http.get(Uri.parse('https://samples.openweathermap.org/data/2.5/weather?q=London&appid=b1b15e88fa797225412429c1c50c122a1'));
print(response);
}