我正在尝试在flutter的小部件的模拟器中显示从数据库的表中获取的数据。 getMethod函数工作正常,因为当我使用打印时,它在控制台上显示了结果。我使用的是Futurebuilder,由于我不太熟悉它,所以我只复制了在教程中发现的似乎正确的代码。 (我使用PHP作为后端语言)。有谁知道哪里出了问题?这是我使用的FutureBuilder
FutureBuilder(
future: getMethod(),
builder: (BuildContext context, AsyncSnapshot snapshot){
List snap = snapshot.data;
if(snapshot.connectionState == ConnectionState.waiting){
return Center(
child: CircularProgressIndicator(),
);
}
if(snapshot.hasError){
return Center(
child: Text("Error"),
);
}
return ListView.builder(
itemCount: snap.length,
itemBuilder: (context,index){
return ListTile(
title: Text("head: ${snap[index]['userName']}"),
subtitle: Text("body ${snap[index]['password']}"),
);
},
);
}
),
这是getMethod
Future getMethod() async{
String theUrl = 'http://192.168.0.117/testlocalhost/annonces/getData.php';
var response = await http.get(
Uri.encodeFull(theUrl),
headers: {
"Accept" : "application/json"
}
);
var reponseBody = json.decode(response.body);
print(reponseBody);
return reponseBody;
}
这是从表中获取数据的php代码
<?php
require('TestLocalHost.php');
$makeQuery = "SELECT * FROM user";
$stamement = $connection->prepare($makeQuery);
$stamement->execute();
$myarray = array();
while($resultsFrom = $stamement-> fetch()){
array_push($myarray,array(
"userName"=>$resultsFrom['User_name'],
"password"=>$resultsFrom['password']
)
);
}
echo json_encode($myarray);
?>
那是我使用打印时显示的内容
connected [{“ userName”:“ wiam_elh”,“ password”:“ wiam”}]