Search⌘ K
AI Features

Solution: Crypto API

Explore how to connect your Flutter app to a Crypto API by implementing HTTP GET requests and handling asynchronous data with FutureBuilder. Learn to display live data, manage loading states, and handle errors effectively to create a responsive user experience.

Solutions

Great job on completing all the steps in the previous challenge! Feel free to compare your code solutions with the solutions below:

lib/utilities/coins_service.dart
lib/presentation/coins_screen.dart
import 'dart:convert';
import 'package:http/http.dart';
class CoinsService{
static final CoinsService _singleton = CoinsService._internal();
CoinsService._internal();
static CoinsService get instance => _singleton;
final _baseURL = 'https://44a3d77f-08c1-4a76-8073-52e7d46c888b.mock.pstmn.io/';
Future<List<dynamic>> getCoins()async{
const coinsEndpoint = '/coins/markets';
Response res = await get(Uri.parse(_baseURL + coinsEndpoint));
if (res.statusCode == 200) {
var body = jsonDecode(res.body);
return body;
} else {
throw "Unable to retrieve coins";
}
}
}
Crypto API solution code

Challenge 1: Get

...