【问题标题】:Flutter PARAMS on Get http获取 http 上的颤振参数
【发布时间】:2020-01-23 18:23:06
【问题描述】:

如何在 GET 函数上放置一个参数, 我在 dart 或 flutter 的文档中没有找到任何关于此的内容

String url = 'xxxxxx';

Future<String> makeRequest() async {

Map params = {
  "anunciante_name"
  "anunciante_pass"
  "anunciante_email"
};

var _body = jsonDecode(params);

var response = await http
.get(Uri.encodeFull(url), headers: {"Content-type": "application/json", "key": "xxxxx", "token": "xxxxx"},);

print(response.body);

【问题讨论】:

    标签: http flutter dart


    【解决方案1】:

    根据documentation for the get method of the http对象,

    Future<Response> get (dynamic url, {Map<String, String> headers})
    

    第二个参数是headers。您可以将您的每个参数作为值传递给它,就像您传递 keytokenContent-Type 一样。

    Map<String, String> reqHeaders = {
        "Content-type": "application/json", 
        "key": "xxxxx", 
        "token": "xxxxx", 
        "anunciante_name":name,
        "anunciante_pass":password,
        "anunciante_email":email
    };
    
    var response = await http.get(Uri.encodeFull(url), {headers: reqHeaders});
    
    

    【讨论】:

    • 你救了我……还有 1 个问题……每次我尝试使用 response.statusCode == 200 时,它都会说 statusCode 不是我尝试创建的变量,但它仍然是一个错误,并且在互联网示例中,没有人定义变量 statusCode 他们只是使用它,就好像它是 dart 原生的东西
    • 好的@EricMontelares,我得看看你的代码。接收带有 statusCode 属性的响应对象不依赖于您的 Flutter/前端代码。服务器 api 附加 statusCode。检查 api 的 api 文档以查看 statusCode 属性是否附加到响应中。还要检查 ok 属性是否附加到响应中。有时,会将值为 truefalseok 属性附加到响应,而不是 200 的 statusCode
    • 我已经决定了,真的如你所说,再次感谢一切
    猜你喜欢
    • 2021-03-17
    • 2022-06-30
    • 2019-04-17
    • 1970-01-01
    • 1970-01-01
    • 2021-03-21
    • 2020-05-10
    • 2020-07-20
    • 1970-01-01
    相关资源
    最近更新 更多