0

this is the function to call and get the data

Future<CoinGeckoResult<prefix.Coin?>> getCoinData() async {
   CoinGeckoApi api = CoinGeckoApi();

   var apiDatabitcoin = await api.coins.getCoinData(id:'bitcoin');

  return apiDatabitcoin;
 }

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

mary
  • 37
  • 7

1 Answers1

0

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

Kaushik Chandru
  • 15,510
  • 2
  • 12
  • 30
  • Future getData() async { final api = CoinGeckoApi(); final result = await api.coins.getCoinData( id: 'bitcoin', ); var jsonDecode= json.decode(result); return jsonDecode['data']['image']['large']; print(result); } it gives an error for result that cant be assign to the string because it is CoinGeckoResult – mary Jun 07 '22 at 14:07
  • Edited my answer. Please check – Kaushik Chandru Jun 07 '22 at 14:38