为了账号安全,请及时绑定邮箱和手机立即绑定

使用 dart 向 php 发送 Http 请求

使用 dart 向 php 发送 Http 请求

PHP
慕桂英546537 2021-11-26 16:02:28
我尝试使用 dart 将数据发布到 php 后端。以某种方式使用 ajax,我可以获得响应,但是使用 dart,它显示 <b>Notice</b>:  Trying to get property 'email' of non-object in <b>D:\xampp\htdocs\PHPBackend\api\login\login_account.php</b> on line <b>23</b><br /><b>Notice</b>:  Trying to get property 'password' of non-object in <b>D:\xampp\htdocs\PHPBackend\api\login\login_account.php</b> on line <b>24</b><br />这些是我用于登录的 php 代码。// login_account.php$database = new Database();$db = $database->getConnection();$login = new Login($db);$data = json_decode(file_get_contents("php://input"));$login->email = $data->email;$login->password = $data->password;$login->loginAccount();$login_arr = array(  "email" => $login->email,  "password" => $login->password);print_r(json_encode($login_arr));?>// login.phpfunction loginAccount(){    // query to read single record    $query = "SELECT    email, password    FROM    " . $this->table_name . " WHERE    email = :email AND password = :password";    // prepare query statement    $stmt = $this->conn->prepare( $query );    // sanitize    $this->email=htmlspecialchars(strip_tags($this->email));    $this->password=htmlspecialchars(strip_tags($this->password));    // bind id of food to be updated    $stmt->bindParam(":email", $this->email);    $stmt->bindParam(":password", $this->password);    die($this->email);    die($this->password);    // execute query    $stmt->execute();    // get retrieved row    $row = $stmt->fetch(PDO::FETCH_ASSOC);    // set values to object properties    $this->email = $row['email'];    $this->password = $row['password'];  }}
查看完整描述

2 回答

?
MM们

TA贡献1886条经验 获得超2个赞

附加信息


有一个 dart 包为 http 请求提供了一些帮助类。


Github:https : //github.com/Ephenodrom/Dart-Basic-Utils


安装它:


dependencies:

  basic_utils: ^1.5.1

它也是 EZ-Flutter Collection 的一部分:


Github:https : //github.com/Ephenodrom/EZ-Flutter 文档:https : //ez-flutter.de/docs


dependencies:

  ez_flutter: ^0.2.5

用法


Map<String, String> headers = {

  "Some": "Header"

};

Map<String, String> queryParameters = {

  "Some": "Parameter"

};


String url = "";

Map payload = "{}";


    Map<String, dynamic> reaponseBody;

    try {

        responseBody = await HttpUtils.postForJson(url, json. encode(payload) ,

        queryParameters: queryParameters, headers: headers);

    } catch (e) {

        // Handle exception, for example if response status code != 200-299

    }

    // do something with the response 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);


查看完整回答
反对 回复 2021-11-26
?
幕布斯6054654

TA贡献1876条经验 获得超7个赞

我可以在 Dart 代码中看到您正在尝试直接发送 Map 对象,而不是首先将其转换为例如 JSON。

要转换为 JSON,您可以使用 dart:convert 包和以下方法:

var encodedLoginObj = json.encode(loginObj);


查看完整回答
反对 回复 2021-11-26
  • 2 回答
  • 0 关注
  • 184 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信