4

如果我尝试在 Postman 上运行,它可以完美运行。看下图。

在此处输入图像描述

可以看到,下面是url

https://xx.yy/api/user/:slug

路径参数是

蛞蝓

我在 Flutter 中的代码,不起作用!

    final _authority = "xx.yy";
    final _path = "api/user/:slug"; // Tried to replace "api/user/slug" AND "api/user"
    final _params = { "slug" : "govadiyo" };
    final _uri =  Uri.https(_authority, _path, _params);

    print(Uri.encodeFull(_uri.toString()));
    var response = await http.get(Uri.encodeFull(_uri.toString()), headers: {'Content-Type': 'application/json'});
    print(response.body);

上面的代码有什么问题吗?

4

3 回答 3

4

正如您正确注意到的那样,您需要一个路径变量,而不是查询参数(这意味着您的变量成为 url 的一部分)。

您可以使用字符串插值将变量放入 url(实际上,串联也可以)。该变量可能包含需要编码的字符。

final slug = 'govadiyo';
final url = Uri.encodeFull('api/user/${slug}');
print(url);
于 2019-06-14T05:26:58.417 回答
2

看看这个答案。似乎这个问题和你的差不多: https ://stackoverflow.com/a/52824562/11620670

摆脱 _path 变量中的参数。

_uri 变量似乎结构良好。

在这个小改动之后,它应该可以工作。链接答案中的示例也是如此。

问候

于 2019-06-11T05:46:53.987 回答
0

在 2021 年像这样在 http.get() 中发送数据的最佳方式您可以发送任何类型的数据,例如 Map、String 任何您想要的类型,只需将数据放入 sendNotification 参数

http://www.google.com/hitnotification?sender_name=fahad&email=fahad@gmail.com&receiver_id=2`


 String sendNotification = "your data";

final uri = Uri.http('www.google.com','/hitnotification'+sendNotification);

       await http.get(uri);
于 2021-02-24T12:03:28.293 回答