-1

i want to use multiple parametres for restful api in flutter.

i can get data by id but i want to get data by id and userId. how could i do?

class NetworkService {

  Future<List<dynamic>> fetchData(int id) async {
    final response = await http.get(Uri.parse
    ("https://jsonplaceholder.typicode.com/posts?id=$id"));

    if (response.statusCode == 200) {
      return jsonDecode(response.body);
    } else {
      print("Failed to load data");
    }
  }
}
Ozan
  • 53
  • 6

2 Answers2

0

You could do this

class NetworkService {

  Future<List<dynamic>> fetchData(int id, int userId) async {
    final response = await http.get(Uri.parse
    ("https://jsonplaceholder.typicode.com/posts?id=$id&userId=$userId"));

    if (response.statusCode == 200) {
      return jsonDecode(response.body);
    } else {
      print("Failed to load data");
    }
  }
}
Ivo
  • 18,659
  • 2
  • 23
  • 35
0

Try the following

  Future<List<dynamic>> fetchData(int id, int userId) async {

  Map parameters = {"id": id, "userId": userId};
  final request =
      Request('GET', Uri.parse("https://jsonplaceholder.typicode.com/posts"));
  request.headers['content-type'] = 'application/json';
  request.body = json.encode(parameters);
  final response = await request.send();
  final res = await Response.fromStream(response);

  if (res.statusCode == 200) {
    return jsonDecode(res.body);
  } else {
    print("Failed to load data");
  }
}

  

  
Sukaina Ahmed
  • 112
  • 1
  • 10