[SOLVED] Flutter Fetching Crypto Data from API


This Content is from Stack Overflow. Question asked by Yheu-Shen

There are no errors shown in my code. I don’t understand why the list of names of crypto assets under ListView.builder aren’t showing on my app screen?

All I see in my ‘Hello World’ page is an empty screen.

My code is modified from this tutorial:

import 'package:flutter/material.dart';
import '../models/asset.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
import '../widgets/searchPage.dart';

class AddAssetScreen extends StatefulWidget {
  _AddAssetScreenState createState() => _AddAssetScreenState();

class _AddAssetScreenState extends State<AddAssetScreen> {
  List<Asset> _assets = [];

  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Hello World'),
        actions: <Widget>[
            icon: Icon(Icons.search),
            onPressed: () {
                  builder: (ctx) => SearchPage(
                        assets: _assets,
      body: FutureBuilder(
        future: _getData(),
        builder: (ctx, snapshot) {
          if (snapshot.connectionState == ConnectionState.waiting)
            return CircularProgressIndicator();
          return ListView.builder(
            itemBuilder: (ctx, i) {
              return Column(
                mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                    title: Text(_assets[i].name!),
                    height: 0,
            itemCount: _assets.length,

  Future<void> _getData() async {
    var url =
    http.get(Uri.parse(url)).then((data) {
      return json.decode(data.body);
    }).then((data) {
      for (var json in data) {
    }).catchError((e) {

The asset model code is as follows:

class Asset {
  String? id;
  String? name;
  String? image;
  double? currentPrice;
  double? priceChange24h;
  double? priceChangePercentage24h;

    required this.id,
    required this.name,
    required this.image,
    required this.currentPrice,
    required this.priceChange24h,
    required this.priceChangePercentage24h,

  Asset.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    name = json['name'];
    image = json['image'];
    currentPrice = json['current_price'];
    priceChange24h = json['price_change_24h'];
    priceChangePercentage24h = json['price_change_percentage_24h'];

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['name'] = this.name;
    data['image'] = this.image;
    data['current_price'] = this.currentPrice;
    data['price_change_24h'] = this.priceChange24h;
    data['price_change_percentage_24h'] = this.priceChangePercentage24h;
    return data;


_getData needs to return something other than void like a List.

This Question was asked in StackOverflow by YSC and Answered by returnVoid It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?