从Uri中提取参数/值对作为Map

时间:2012-08-02 08:33:51

标签: dart

如何使用Dart获取URL / URI的参数/值对?不幸的是,目前在 Uri 库或 Location 界面中都没有针对此问题的内置功能。<​​/ p>

4 个答案:

答案 0 :(得分:2)

您可以使用Uri.splitQueryString将查询拆分为地图。

答案 1 :(得分:2)

现在有一个Uri的queryParameters成员返回一个地图

Uri u = Uri.parse("http://app.org/main?foo=bar&baz=bat");
Map<String,String> qp = u.queryParameters;
print(qp);

// {foo: bar, baz: bat}

答案 2 :(得分:0)

  Map<String, String> getUriParams(String uriSearch) {
    if (uriSearch != '') {
      final List<String> paramValuePairs = uriSearch.substring(1).split('&');

      var paramMapping = new HashMap<String, String>();
      paramValuePairs.forEach((e) {
        if (e.contains('=')) {
          final paramValue = e.split('=');
          paramMapping[paramValue[0]] = paramValue[1];
        } else {
          paramMapping[e] = '';
        }
      });
      return paramMapping;
    }
  }

// Uri: http://localhost:8080/incubator/main.html?param=value&param1&param2=value2&param3
  final uriSearch = window.location.search;
  final paramMapping = getUriParams(uriSearch);

答案 3 :(得分:0)

 // url=http://127.0.0.1:3030/path/Sandbox.html?paramA=1&parmB=2#myhash
void main() {
   String querystring = window.location.search.replaceFirst("?", "");
   List<String> list = querystring.split("&").forEach((e) => e.split("="));
   print(list); // [[paramA, 1], [parmB, 2]]
}