class _MyHomePageState extends State<MyHomePage> { final _controller = TextEditingController(); static const host = 'baconipsum.com'; static const path = '/api/?type=meat-and-filler¶s=1&format=text'; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Home'), ), body: Padding( padding: EdgeInsets.all(20.0), child:Column( children:<Widget> [ Text('INTERNET ACCESS', style: TextStyle(fontSize: 32, fontWeight: ui.FontWeight.w500), ), Padding(padding: EdgeInsets.all(10.0)), TextField( controller: _controller, style: TextStyle(fontSize: 24), minLines: 1, maxLines: 5, ), ], ), ), floatingActionButton: FloatingActionButton( child: Icon(Icons.open_in_new), onPressed: () { getData(); showDialog( context: context, builder: (BuildContext context) => AlertDialog( title: Text("loaded!"), content: Text("get content from URI."), ) ); } ) ); } void getData() async { var http = await HttpClient(); HttpClientRequest request = await http.get(host, 80, path); HttpClientResponse response = await request.close(); final value = await response.transform(utf8.decoder).join(); _controller.text = value; } }
void setData() async { final ob = { "title": "foo", "author": "hoge", "content": "this is content.サンプルコンテンツ" }; final jsondata = json.encode(ob); var https = await HttpClient(); HttpClientRequest request = await https.postUrl(Uri.parse(url)); request.headers.set(HttpHeaders.contentTypeHeader, "application/json; charset=UTF-8"); request.write(jsondata); HttpClientResponse response = await request.close(); final value = await response.transform(utf8.decoder).join(); _controller.text = value; }