skip to Main Content

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

2

Answers


  1. 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");
        }
      }
    }
    
    Login or Signup to reply.
  2. 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");
      }
    }
    
      
    
      
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search