如何在 Dart 中的 Uri 查询字符串中有重复的键

TSR*_*TSR 1 google-maps google-maps-api-3 google-maps-markers dart flutter

应用:谷歌地图静态API

为了添加多个标记,文档说我们只需要设置markers查询字符串的多个值

多个标记可以放置在同一个标​​记参数中,只要它们表现出相同的样式;您可以通过添加其他标记参数来添加不同样式的其他标记。

我尝试像这样使用 Uri lib:

 final url = new Uri(
      scheme: 'https',
      host: 'maps.googleapis.com',
      path: 'maps/api/staticmap',
      queryParameters: {
        'markers' : 'color:blue|label:C|1.015,1.054',
        'markers' : 'color:red|label:C|1.012,1.057',
      },
    );
 print(url.toString());
Run Code Online (Sandbox Code Playgroud)

但是 dart 不允许这样做。还有其他方法吗?

发生的情况是它只显示一个标记。(首先)

jam*_*lin 5

AMap不能包含重复的键。但是,Uri的构造函数确实支持生成具有重复键的查询字符串。从的构造函数的文档中Uri

queryParameters使用的查询被从提供地图内置....在映射中的值必须是字符串或Iterable字符串组成,其中后者对应于多个值相同的密钥。

所以你可以这样做:

final url = Uri(
  scheme: 'https',
  host: 'maps.googleapis.com',
  path: 'maps/api/staticmap',
  queryParameters: {
    'markers': [
      'color:blue|label:C|1.015,1.054',
      'color:red|label:C|1.012,1.057'
    ],
  },
);
print(url.toString());
Run Code Online (Sandbox Code Playgroud)

打印:

https://maps.googleapis.com/maps/api/staticmap?markers=color%3Ablue%7Clabel%3AC%7C1.015%2C1.054&markers=color%3Ared%7Clabel%3AC%7C1.012%2C1.057
Run Code Online (Sandbox Code Playgroud)