如何在从 API 获取数据到 Flutter 时使用多个参数获取数据。

huangapple go评论136阅读模式
英文:

how to i get data with multiple parametres while get data from api to flutter

问题

我想在Flutter中的RESTful API中使用多个参数。

我可以按ID获取数据,但我想按ID和UserID获取数据。
我该怎么做?

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("无法加载数据");
    }
  }
}
英文:

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&lt;List&lt;dynamic&gt;&gt; fetchData(int id) async {
    final response = await http.get(Uri.parse
    (&quot;https://jsonplaceholder.typicode.com/posts?id=$id&quot;));

    if (response.statusCode == 200) {
      return jsonDecode(response.body);
    } else {
      print(&quot;Failed to load data&quot;);
    }
  }
}

答案1

得分: 0

你可以这样做

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("加载数据失败");
    }
  }
}
英文:

You could do this

class NetworkService {

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

    if (response.statusCode == 200) {
      return jsonDecode(response.body);
    } else {
      print(&quot;Failed to load data&quot;);
    }
  }
}

</details>



# 答案2
**得分**: 0

以下是您要翻译的内容:

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

如果您有任何其他需要,请随时提出。

英文:

Try the following

  Future&lt;List&lt;dynamic&gt;&gt; fetchData(int id, int userId) async {

  Map parameters = {&quot;id&quot;: id, &quot;userId&quot;: userId};
  final request =
      Request(&#39;GET&#39;, Uri.parse(&quot;https://jsonplaceholder.typicode.com/posts&quot;));
  request.headers[&#39;content-type&#39;] = &#39;application/json&#39;;
  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(&quot;Failed to load data&quot;);
  }
}

huangapple
  • 本文由 发表于 2023年3月7日 16:53:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75659768.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定