Efe Bozkurt
Efe Bozkurt

Reputation: 1

Can't Fetch Data from Coinmarketcap API

I am trying to get data from Coinmarketcap API and show it in a listview. I tried the same code with mock API's and it is working but I couldn't make it work with this API.

My API credit count is going up so I assume that the getting data part of the code is true but maybe I can't show it in a listview, I don't really know the problem so maybe you can help.

Code Response

Code response with mock API

Source code:

void main() {
  // runApp(const PortfolioApp());
  runApp(const DENEME2());
}


//Fetching data from the API
Future<List<CryptoModel>> getCryptoMarket() async {
  const url =
      'https://pro-api.coinmarketcap.com/v2/tools/price-conversion?amount=1&id=1';

  var response = await http.get(Uri.parse(url), headers: {
    'Accepts': 'application/json',
    'X-CMC_PRO_API_KEY': 'YOUR KEY HERE'
  });
  final List body = json.decode(response.body);
  return body.map((e) => CryptoModel.fromJson(e)).toList();
}

Future<List<CryptoModel>> postsFuture = getCryptoMarket();

//Showing the data 
class DENEME2 extends StatefulWidget {
  const DENEME2({super.key});

  @override
  State<DENEME2> createState() => _DENEME2State();
}

class _DENEME2State extends State<DENEME2> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: FutureBuilder<List<CryptoModel>>(
            future: postsFuture,
            builder: (context, snapshot) {
              if (snapshot.connectionState == ConnectionState.waiting) {
                //WORKED
                return const CircularProgressIndicator();
              } else if (snapshot.hasData) {
                //DID NOT WORKED
                final posts = snapshot.data!;
                return buildPosts(posts);
              } else {
                //WORKED
                return const Text("No data available");
              }
            },
          ),
        ),
      ),
    );
  }
}

Widget buildPosts(List<CryptoModel> posts) {
  return ListView.builder(
    itemCount: posts.length,
    itemBuilder: (context, index) {
      final post = posts[index];
      return Container(
        color: Colors.grey.shade300,
        margin: EdgeInsets.symmetric(vertical: 5, horizontal: 10),
        padding: EdgeInsets.symmetric(vertical: 5, horizontal: 5),
        height: 100,
        width: double.maxFinite,
        child: Row(
          children: [
            Expanded(flex: 1, child: Text(post.data.symbol)),
            SizedBox(width: 10),
            Expanded(flex: 3, child: Text(post.data.name)),
          ],
        ),
      );
    },
  );
}


API response on postman:

{
    "status": {
        "timestamp": "2024-03-20T10:30:14.348Z",
        "error_code": 0,
        "error_message": null,
        "elapsed": 44,
        "credit_count": 1,
        "notice": null
    },
    "data": {
        "id": 1,
        "symbol": "BTC",
        "name": "Bitcoin",
        "amount": 1,
        "last_updated": "2024-03-20T10:29:00.000Z",
        "quote": {
            "USD": {
                "price": 63192.89078082416,
                "last_updated": "2024-03-20T10:29:00.000Z"
            }
        }
    }
}

Upvotes: 0

Views: 79

Answers (0)

Related Questions