1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| Future<Article3> futureArticle3;
@override void initState() { super.initState(); futureArticle3=_httpGet2(); } //显示 FutureBuilder<Article3>( future: futureArticle3, builder: (context, snapshot) { if (snapshot.hasData) { return ListView.separated( itemCount: snapshot.data.data.datas.length, separatorBuilder: (context, index) {return Divider(height: 10, color: Colors.grey,);}, itemBuilder: (BuildContext context, int index) { return Column( children: [ Text("${snapshot.data.data.datas[index].author}"), Text("${snapshot.data.data.datas[index].title}"), Text("${snapshot.data.data.datas[index].niceShareDate}") ], ); }, ); } else if (snapshot.hasError) { return Text("${snapshot.error}"); } return CircularProgressIndicator(); }, ) //网络请求 Future<Article3> _httpGet2() async { var httpClient = new HttpClient(); var uri = Uri( scheme: 'https', host: 'www.wanandroid.com', path: 'article/list/0/json', );
HttpClientRequest request = await httpClient.getUrl(uri); HttpClientResponse response = await request.close(); String responseBody = await response.transform(utf8.decoder).join(); var jsonDecode = json.decode(responseBody); var article3=Article3.fromJson(jsonDecode); print(article3); return article3; }
|