标签:build 转化 网络 app state == nbsp list() over
import ‘package:flutter/material.dart‘;
import ‘package:http/http.dart‘ as http;
import ‘dart:convert‘;
class HttpPage extends StatefulWidget {
HttpPage({Key key}) : super(key: key);
_HttpDemoState createState() => _HttpDemoState();
}
class _HttpDemoState extends State {
List _list=[];
@override
initState() {
// TODO: implement initState
super.initState();
this._getData();
}
_getData() async{
var apiUrl="http://a.itying.com/api/productlist";
var result=await http.get(apiUrl);
if(result.statusCode==200){
print(result.body);
setState(() {
this._list=json.decode(result.body)["result"];
});
}else{
print("失败${result.statusCode}");
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("请求数据Demo"),
),
body: this._list.length>0?ListView(
children: this._list.map((value){
return ListTile(
title: Text(value["title"]),
);
}).toList(),
):Text("加载中...")
);
}
}
标签:build 转化 网络 app state == nbsp list() over
原文地址:https://www.cnblogs.com/zhaofeis/p/12359505.html