1

I want to send a GET http request with parameters, my problem is that when I add the parameters in the request URL manually it works fine, but when I pass them as parameters it returns an exception without any explanation and somehow the execution stops after Uri.https

here is the code that I want to achieve

  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;
  }

and here is the manual way which shouldn't be applied

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

I have also tried the dio library from pub.dev but also wasn't helpful.

And finally thanks in advance to everyone

Ahmed Gamal
  • 55
  • 1
  • 10

4 Answers4

0

You may try this

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);

Sowat Kheang
  • 668
  • 2
  • 4
  • 12
0
import 'package:http/http.dart' as http;

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

Just modify your GET request like this.

Faizan Darwesh
  • 301
  • 1
  • 5
0

Try this

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 !");
  }
}
Anand
  • 4,355
  • 2
  • 35
  • 45
0

Query parameters don't support bool type. Use String instead: 'true'.

A value in the map must be either a string, or an Iterable of strings, where the latter corresponds to multiple values for the same key.

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

See Uri constructor for details.

user18309290
  • 5,777
  • 2
  • 4
  • 22