Home > Net >  How to get SteamApp list in flutter/dart?
How to get SteamApp list in flutter/dart?

Time:03-21

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?

CodePudding user response:

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 
  • Related