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 50 51 52 53 54
| Future<Article2> futureArticle2;
@override void initState() { super.initState(); futureArticle2=_httpGet2(); } //显示 FutureBuilder<Article2>( future: futureArticle2, 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<Article2> _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 article2=Article2.fromJson(jsonDecode); print(article2);
// var article = Article.formJson(jsonDecode); // print(article); // return article;
return article2; }
|