To decode the data and extract the body from the json reponse we can use the following function:

import 'dart:convert';

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

class NetworkHelper {
  NetworkHelper({@required this.url});

  final String url;

  Future getDecodedAPI() async {
    http.Response response = await http.get(url);

    if (response.statusCode.toString().startsWith('2')) {
      String data = response.body;
      return jsonDecode(data);
    } else {
      print('Something went wrong');
    }
  }
}

 

 

an example of the function above being used in practice:

import 'package:bitcoin_ticker/services/networking.dart';

class BitcoinModel {
  Future<dynamic> getData(String crypto, String currency) async {
    String url =
        'https://rest.coinapi.io/v1/exchangerate/$crypto/$currency?apiKey=$kApiKey';
    NetworkHelper networkHelper = NetworkHelper(url: url);

    var result = await networkHelper.getDecodedAPI();

    return result;
  }
}