import 'package:http/http.dart' as http;
var url = Uri.parse('https://example.com/whatsit/create');
var response = await http.post(url, body: {'name': 'doodle', 'color': 'blue'});
print('Response status: ${response.statusCode}');
print('Response body: ${response.body}');
print(await http.read('https://example.com/foobar.txt'));
dependencies:
http: ^0.12.0+4
http: ^0.13.4
dependencies:
http:^0.12.2
import 'package:http/http.dart';
dependencies:
http: ^0.12.1
import 'dart:convert';
import 'package:http/http.dart' as http;
void main {
final response = await http.get(Uri.parse("https://domain.com/endpoint?data=hello"));
String responseData = utf8.decode(response.bodyBytes);
print(json.decode(responseData));
}
dependencies:
http: ^0.13.3
http: ^0.13.1
var client = http.Client();
try {
var uriResponse = await client.post(Uri.parse('https://example.com/whatsit/create'),
body: {'name': 'doodle', 'color': 'blue'});
print(await client.get(uriResponse.bodyFields['uri']));
} finally {
client.close();
}
http: ^0.12.2
import 'package:http/http.dart' as http;
var url = Uri.parse('https://example.com/whatsit/create');
var response = await http.post(url, body: {'name': 'doodle', 'color': 'blue'});
print('Response status: ${response.statusCode}');
print('Response body: ${response.body}');
print(await http.read(Uri.parse('https://example.com/foobar.txt')));
class UserAgentClient extends http.BaseClient {
final String userAgent;
final http.Client _inner;
UserAgentClient(this.userAgent, this._inner);
Future<http.StreamedResponse> send(http.BaseRequest request) {
request.headers['user-agent'] = userAgent;
return _inner.send(request);
}
}
dev_dependencies:
flutter_test:
sdk: flutter
http: ^0.12.0+4
provider_architecture: ^1.0.5
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:flutter_app/data/post.dart';
// Uncomment lines 7 and 10 to view the visual layout at runtime.
// import 'package:flutter/rendering.dart' show debugPaintSizeEnabled;
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
MyApp({Key key}) : super(key: key);
@override
_MyAppState createState() => _MyAppState();
}
Future<Post> fetchPost() async {
final response = await http.get(Uri.https('jsonplaceholder.typicode.com', '/posts/1'));
if (response.statusCode == 200) {
// 만약 서버가 OK 응답을 반환하면, JSON을 파싱합니다.
return Post.fromJson(json.decode(response.body));
} else {
// 만약 응답이 OK가 아니면, 에러를 던집니다.
throw Exception('Failed to load post');
}
}
class _MyAppState extends State<MyApp> {
Future<Post> post;
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Fetch Data Example',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
appBar: AppBar(
title: Text('Fetch Data Example'),
),
body: Center(
child: FutureBuilder<Post>(
future: post,
builder: (context, snapshot) {
if (snapshot.hasData) {
return Text(snapshot.data.title);
} else if (snapshot.hasError) {
return Text("${snapshot.error}");
}
// 기본적으로 로딩 Spinner를 보여줍니다.
return CircularProgressIndicator();
},
),
),
),
);
}
@override
void initState() {
super.initState();
post = fetchPost();
}
}
$ dart pub add http