Asm*_*oun 4 php mysql json dart flutter
我的主要问题是下面的代码对我来说工作正常,但没有优化,我有一个包含以下 MySQL 请求的 PHP 文件:
if("GET_CLIENT" == $action){
$code = $_POST['code'];
$db_data = array();
$sql = "SELECT `nom` , `prenom`, `age` FROM `client` WHERE `code` LIKE '$code'" ;
$result = $conn->query($sql);
$row = $result->fetch_assoc())
echo json_encode($db_data);
$conn->close();
return;}
Run Code Online (Sandbox Code Playgroud)
在我的飞镖应用程序中,我有以下课程Client:
class Client {
String code;
String nom;
String prenom;
Client({this.code, this.prenom, this.nom });
factory Client.fromJson(Map<String, dynamic> json) {
return Client(
code: json['code'] as String,
nom: json['nom'] as String,
prenom: json['prenom'] as String, ); } }
Run Code Online (Sandbox Code Playgroud)
现在要从数据库中获取返回的单行,我有以下Future功能:
Future<Client> fetchClient(String code) async {
var map = Map<String, dynamic>();
map['action'] = 'GET_CLIENT';
map['code'] = code;
var response = await http.post(uri, body: map);
if (response.statusCode == 200) {
final items = json.decode(response.body).cast<Map<String, dynamic>>();
List<Client> listOfClients = items.map<Client>((json) {
return Client.fromJson(json);
}).toList();
print(listOfClients.first.code);
return listOfClients.first;
} else {
throw Exception('Failed to load data.');
}
}
Run Code Online (Sandbox Code Playgroud)
这对我来说很好用,但正如你所看到的,该Future函数正在创建一个客户端列表,而这个列表当然只有一个项目,所以我return listOfClients.first;现在使用 我的问题是如何优化我的 Future 函数以仅返回一个客户端,如下所示:
if (response.statusCode == 200) {
final items = json.decode(response.body).cast<Map<String, dynamic>>();
Client client = .. somthing ??
// instead of List Client
return client; // instead of return listOfClients.first
}
Run Code Online (Sandbox Code Playgroud)
PS:这篇文章的标题有点令人困惑,任何建议更改它请编辑
如果Get_only_one_situation方法编写正确,它应该只返回一个值,你只需要decode它,像这样:
const uri = 'http://10.0.2.2/re/App_agent/agent.php';
Future<Situation> fetchOneSituation(String ID) async {
var map = Map<String, dynamic>();
map['action'] = 'Get_only_one_situation';
map['ID'] = ID;
var response = await http.post(uri, body: map);
if (response.statusCode == 200) {
return Situation.fromJson(json.decode(response.body));
// You probably need this
// return Situation.fromJson(json.decode(response.body)['data'])
} else {
throw Exception('Failed to load data.');
}
}
Run Code Online (Sandbox Code Playgroud)
在您更新您的问题后,我很清楚您正在使用此操作获取所有扇区Get_only_one_situation,这不是首选。
如果必须获取整个表,您需要做的就是使用该firstWhere方法获取适当的项目,如下所示:
Future<Situation> fetchSituation(String ID) async {
var map = Map<String, dynamic>();
map['action'] = 'Get_only_one_situation';
var response = await http.post(uri, body: map);
if (response.statusCode == 200) {
final items = json.decode(response.body).cast<Map<String, dynamic>>();
List<Situation> listOfSituations = items.map<Client>((json) {
return Situation.fromJson(json);
}).toList();
return listOfSituations.firstWhere((item)=>item.ID==item.ID);
} else {
throw Exception('Failed to load data.');
}
}
Run Code Online (Sandbox Code Playgroud)
当然,我不推荐这种方法,因为在数据库上查询比flutter中的代码要快,尤其是在这么多数据的情况下。
| 归档时间: |
|
| 查看次数: |
340 次 |
| 最近记录: |