flutter http get 请求带参数

问题描述 投票:0回答:5

我想发送一个带有参数的 GET http 请求,我的问题是,当我手动在请求 URL 中添加参数时,它工作正常,但是当我将它们作为参数传递时,它会返回一个异常,没有任何解释,并且执行之后以某种方式停止Uri.https

这是我想要实现的代码

  Future<List<LawFirm>> getLawFirms () async {
    Map<String, dynamic> parameters = {
      'total': true
    };
    final uri =
    Uri.http('www.vision.thefuturevision.com:5000',
        '/api/law-firm', parameters);
    final response = await http.get(uri);
    var dynamicResponse = jsonDecode(response.body);
    totaLawFirms = await dynamicResponse['total'];
    var lawFirms = await dynamicResponse['data'];
    List<LawFirm> list = List<LawFirm>.from(lawFirms.map((x) => LawFirm.fromJson(x)));
    print(list);
    notifyListeners();
    return list;
  }

这是不应该应用的手动方式

    final response = await get(Uri.parse('$baseURL/law-firm?total=true'));

我也尝试过

dio
中的
pub.dev
库,但也没有帮助。

最后提前感谢大家

flutter http get
5个回答
0
投票

你可以试试这个

Map<String, dynamic> parameters = {
  'total': true
};

var uri = Uri(
  scheme: 'http',
  host: 'www.vision.thefuturevision.com:5000',
  path: '/law-firm',
  queryParameters: parameters,
);

final response = await http.get(uri);


0
投票
import 'package:http/http.dart' as http;

 final response =
          await http.get(Uri.parse("${Constants.baseUrl}endpoint/param1/param2"));

只需像这样修改您的 GET 请求即可。


0
投票

试试这个

import 'package:http/http.dart' as http;


callAPI() async {
  String login = "sunrule";
  String pwd = "api";
  Uri url = Uri.parse(
      "http://vijayhomeservices.in/app/api/index.php?apicall=login&login=$login&password=$pwd");
  final response = await http.get(url);
  if (response.statusCode == 200) {
    final body = json.decode(response.body);
    print(body.toString());
  } else {
    throw Exception("Server Error !");
  }
}

0
投票

查询参数不支持

bool
类型。使用
String
代替:“true”。

映射中的值必须是字符串或字符串的 Iterable,其中后者对应于同一键的多个值。

Map<String, dynamic> parameters = {'total': 'true'};
final uri = Uri.http(
    'www.vision.thefuturevision.com:5000', '/api/law-firm', parameters);
print(uri); // http://www.vision.thefuturevision.com:5000/api/law-firm?total=true

有关详细信息,请参阅 Uri 构造函数


0
投票

我也遇到了同样的问题,希望这对某人有帮助。

fetchLawfirms(param1, param2) async{
    var params = {'param1': param1, 'param2': param2};
    var url = Uri.parse('https://www.vision.thefuturevision.com:5000/api/law-firm/');

    url = Uri.https(url.authority, url.path, params);

    http.Response response = await http.get(url, headers: <String, String>{
    'Content-Type': 'application/json; charset=UTF-8',
    'Authorization': 'Token your_token_string'
    });

    if (response.statusCode == 200) {
      ///data successfully
      var result = jsonDecode(response.body);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.