我使用下面的函数从本地主机获取数据到模拟器,我也使用XAMPP,这个函数在互联网上工作得很好,但不适用于本地主机当我试图在模拟器中使用url (我在函数中使用)时,它工作得很好,并获得了数据
我希望如果有人有解决这个问题的办法,谢谢大家。
Future GetData() async {
Uri url = Uri.http('10.0.2.2', '/mobtech/index.php');
var response = await http.get(url);
var responsebody = jsonDecode(response.body);
return responsebody;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Json Test 1'),
),
body: Container(
child: FutureBuilder(
future: GetData(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasData) {
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (context, i) {
return Container(
child: Text(snapshot.data[i]['title'],
style: TextStyle(fontSize: 20)),
);
});
}
return CircularProgressIndicator();
}),
),
);
}发布于 2021-04-06 20:48:00
你必须把你当前的ip作为主机,下面是一个例子:https://www.youtube.com/watch?v=gRLSfB4r_a8ee
https://stackoverflow.com/questions/66966194
复制相似问题