Flutter 颤振http.get与路径参数问题
如果我试着在邮递员身上跑步,效果会很好。请看下图 您可以看到,下面是url 路径参数为 鼻涕虫 我在颤振中的代码不起作用Flutter 颤振http.get与路径参数问题,flutter,dart,Flutter,Dart,如果我试着在邮递员身上跑步,效果会很好。请看下图 您可以看到,下面是url 路径参数为 鼻涕虫 我在颤振中的代码不起作用 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.h
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);
上面的代码有什么问题吗?看看这个答案。看起来问题和你的差不多: 去掉_path变量中的参数 _uri变量似乎结构良好 在这小小的变化之后,它应该会起作用。链接答案中的示例也是如此
问候语正如您正确注意到的,您需要的是路径变量,而不是查询参数(这意味着您的变量成为url的一部分) 您可以使用字符串插值将变量放入url(事实上,连接也可以)。变量可能包含需要编码的字符
final slug='govadiyo';
最终url=Uri.encodeFull('api/user/${slug}');
打印(url);
2021年在http.get()中发送数据的最佳方式如下
您可以发送任何类型的数据,如Map、String或任何类型的数据,只要将数据放入sendNotification参数中即可
谢谢你的回答,但我很抱歉,我尝试了这一个,但结果相同,在我的问题中,我已经分割了主机和路径。@Govaadiyo,你分割了主机和路径,这完全没问题。我的意思是,您将_路径更改为
api/user
,而不使用/:slug
。你在_params中很好地定义了你的params。我认为这只是路径的问题。在你的第一份打印声明中,你的路径是怎么说的?我按照你的建议删除了,打印方式是“”,但不起作用:(好奇地在邮递员身上工作,但不在dart中。)谢谢你以其他方式帮助我?
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);