1

I'm trying to get the game list from the Steam API. I know that in this link there is a page with all the games/apps in a JSON format: https://api.steampowered.com/ISteamApps/GetAppList/v2/ The thing is that I don't know how to get that JSON inside the app. What I tried is downloading the whole website (html) and looking in the correct html tag for the string content that is the JSON, but I guess that there is a better way to get the content. Also, doing that way I can only make it work in web, when I also want it to work in app version.

Any idea on how to get the content of the page?

Axvemi
  • 13
  • 2

1 Answers1

0

You have to launch a request to that API which will give you a response that you can store in a variable.

If the response is in JSON format and you want to treat it as such, you will have to decode it (basically transform from simple text to JSON)

To launch a request you can use Dart's http package which you can import like this:

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

You then make a call to a get method (or in other cases you would use a POST, PUT, DELETE ... method) by passing it the API uri using the Uri.parse() method.
Store the response in a variable.

final response = await http.get( Uri.parse('https://api.steampowered.com/ISteamApps/GetAppList/v2/'));

Now the variable "response" will hold the output of what the server at the API you provided will return (amongst other information)

To get the content, you have to access response.body(other useful information inside of "response" will be response.statusCode that gives you as the name indicates the status of your response which can tell you about errors, successful requests...)

Finally tranform the content into JSON using the json.decode()

import 'dart:convert';//<-- you need to import to be able to use the function
final jsonResponse = json.decode(response.body)

The overall code would look something like this:

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

final response = await http.get( Uri.parse('https://api.steampowered.com/ISteamApps/GetAppList/v2/'));
final jsonResponse = json.decode(response.body)
//Do whatever you want with jsonResponse 
Primata Lógico
  • 725
  • 4
  • 12
  • Hi! Thank you for the answer. Very detailed and well explained. I have only one question. I'm trying to run the code, the thing is that I'm getting the next error running it on a browser: Error: XMLHttpRequest error. C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/patch/core_patch.dart 910:28 get current packages/http/src/browser_client.dart 69:22 – Axvemi Mar 21 '22 at 03:00
  • @Axvemi Might be related to CORS but you have no control over the API so not much you could do. Try to "_disable-web-security_" since I believe this won't be a production app. You'll find the steps here in the 3rd answer: https://stackoverflow.com/questions/64458696/in-flutter-web-getting-xmlhttprequest-error-while-making-http-call – Primata Lógico Mar 21 '22 at 22:22
  • Yeah, It's that. It works on android and desktop versions, the problem it's only on web so it should not be a problem since i'm not going to use that one. Thanks! – Axvemi Mar 21 '22 at 23:32