Home > OS >  calling coin gecko for image of bitcoin in flutter app
calling coin gecko for image of bitcoin in flutter app

Time:06-08

this is the function to call and get the data

Future<String?> getData() async {

final api = CoinGeckoApi();

final result = await api.coins.getCoinData( id: 'bitcoin',

); return result.data!.image?.large;

print(result); }

and this is the widget to show the image:

child: FutureBuilder(
                                            future:getData() ,
                                            builder: (context, snapshot) {
                                            return Container(
                                              child: Column(children: [
                                                Image.network(snapshot.data.toString())
                                              ]),
                                            );
                                          })

but im getting this error: enter image description here

CodePudding user response:

import 'package:coingecko_api/coingecko_api.dart';
import 'package:coingecko_api/coingecko_result.dart';
import 'package:coingecko_api/data/coin.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  bool leftClick = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Center(
      child: FutureBuilder(
          future: getCoinData(),
          builder: (context, AsyncSnapshot<CoinGeckoResult<Coin?>> snapshot) {
            return (snapshot.hasData)
                ? Image.network("${snapshot.data!.data!.image!.large}")
                : CircularProgressIndicator();
          }),
    ));
  }
}

Future<CoinGeckoResult<Coin?>> getCoinData() async {
  CoinGeckoApi api = CoinGeckoApi();
  var apiData = await api.coins.getCoinData(id: 'bitcoin');
  print(apiData.data!.image!.large);
  return apiData;
}

Preview

  • Related