我正在学习flutter并尝试构建一个可以显示我今天的课程的移动应用程序。我想了解更多关于数据处理的信息,所以我尝试使用PHP并从数据库中获取数据。
我的目标是将日期值发布到PHP文件中,然后执行SQL查询并以JSON格式获取数据。最后,数据将显示在颤振应用程序中。
我的PHP文件的一部分如下:

$date = $_POST["date"];

$sql = "SELECT * FROM Table WHERE Date = '$date'";

$response=array();
$final=array();
$response["status"] = "fail";
$result = mysqli_query($con, $sql);

while($row=mysqli_fetch_assoc($result)){
   $response["status"] = "success";
   $final[] = $row;
}

$response["result"]=$final;

echo json_encode($response);

从服务器获取数据的apiprovider.dart文件。
import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' show Client;
import 'package:http/http.dart' as http;

class ApiProvider {

  Client client = Client();
  final String apiURL = "my.php";
  final String today = '2019-06-18';

  // Use to fetch today courses.
  Future<CourseListModel> getCourses() async {

    http.post(apiURL, body: {"date": today});

    final response = await client.get(apiURL);

    if (response.statusCode == 200) {
      // If the API call to the server was successful, parse the course list in JSON format
      return CourseListModel.fromJson(json.decode(response.body));
    }
    else {
      // If the API call was fail, throw an error.
      throw Exception('Response content length is ${response.statusCode}, failed to get today courses.');
    }
  }
}

当我运行颤振应用程序时,无法显示课程数据。我认为“today”值不能正确解析为php文件。我做过谷歌搜索,尝试过不同的方法,但还是没用。我不太明白如何处理邮寄方式。有人能给我一些提示或建议来解决这个问题吗?谢谢。
=======更新了======
我用《以弗所书》的方法解决了我的问题。更新的DART文件如下:
class ApiProvider {
...
  Future<CourseListModel> getCourses() async {

    final response = await http.post(apiURL, body:{"date": today});
...
}

谢谢大家:D

最佳答案

你的问题
您发出一个POST请求,然后您发出一个GET请求。您应该从POST请求中的PHP脚本获得响应。之后不需要发出GET请求。
更改此:


http.post(apiURL, body: {"date": today});
final response = await client.get(apiURL);


为此:

final response = http.post(apiURL, body: {"date": today});


我强烈建议您查看以下信息!
附加信息
有一个DART包为HTTP请求提供了一些助手类。
Github:https://github.com/Ephenodrom/Dart-Basic-Utils
安装时使用:
dependencies:
  basic_utils: ^1.5.1

用法
Map<String, String> headers = {
  "Some": "Header"
};
Map<String, String> queryParameters = {
  "Some": "Parameter"
};

String url = "";
String payloadAsString = "{\"date\": \"2019-06-18\"}";

Future<CourseListModel> getCourses() async {

    Map<String, dynamic> body;
    try {
        body = await HttpUtils.postForJson(url, payloadAsString,
        queryParameters: queryParameters, headers: headers);
    } catch (e) {
        // Handle exception, for example if response status code != 200-299
    }
    return CourseListModel.fromJson(body);
}

其他信息:
这些都是httputils类中的方法。
Future<Map<Response> getForFullResponse(String url,{Map<String, dynamic> queryParameters,Map<String, String> headers});
Future<Map<String, dynamic>> getForJson(String url,{Map<String, dynamic> queryParameters,Map<String, String> headers});
Future<String> getForString(String url,{Map<String, dynamic> queryParameters,Map<String, String> headers});
Future<Map<Response> postForFullResponse(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});
Future<Map<String, dynamic>> postForJson(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});
Future<String> postForString(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});
Future<Response> putForFullResponse(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});
Future<Map<String, dynamic>> putForJson(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});
Future<String> putForString(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});
Future<Response deleteForFullResponse(String url,{Map<String, String> queryParameters,Map<String, String> headers});
Future<Map<String, dynamic>> deleteForJson(String url,{Map<String, String> queryParameters,Map<String, String> headers});
Future<String> deleteForString(String url,{Map<String, String> queryParameters,Map<String, String> headers});
Map<String, dynamic> getQueryParameterFromUrl(String url);
String addQueryParameterToUrl(String url, Map<String, dynamic> queryParameters);

10-01 23:48
查看更多