5-1
HTTP請求
- https://pub.dartlang.org/packages/http
- 開啟pubspec.yaml在dependencies下加入:
- 完整範例:
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'http請求範例',
home: new Scaffold(
appBar: new AppBar(
title: new Text('http請求範例'),
),
body: new Center(
child: new RaisedButton(
onPressed: (){
var url='https://schoolweb.tn.edu.tw';
http.get(url).then((response){
print("狀態:${response.statusCode}");
print("正文:${response.body}");
});
},
child: new Text('發起http請求'),
),
),
),
);
}
}
執行結果可以從編輯器下方的「偵錯主控台」觀看(並不會呈現在主機上)