I have a api that returns a list of data. When I retrieve that data I use a FutureBuilder to show the list of data. But for some reason it won't show my data even though when I print the response I can see that I got a correct response.

This is the error that i got:

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (11846): The following assertion was thrown building FutureBuilder<List<BasicDiskInfo>>(dirty, state:
I/flutter (11846): _FutureBuilderState<List<BasicDiskInfo>>#a0948):
I/flutter (11846): A build function returned null.
I/flutter (11846): The offending widget is: FutureBuilder<List<BasicDiskInfo>>
I/flutter (11846): Build functions must never return null. To return an empty space that causes the building widget to
I/flutter (11846): fill available room, return "new Container()". To return an empty space that takes as little room as
I/flutter (11846): possible, return "new Container(width: 0.0, height: 0.0)".

I don't know what to do. Help me?

API

static Future<List<BasicDiskInfo>> fetchAllDisks() async {
    final response = await http.get('link');

    if (response.statusCode == 200) {
      Iterable list = json.decode(response.body);
      var disks = new List<BasicDiskInfo>();
      disks = list.map((model) => BasicDiskInfo.fromJson(model)).toList();
      print(disks[0]);
      return disks;
    } else {
      throw Exception('Failed to load disks');
    }
  }

Page

class Disks extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: API.fetchAllDisks(),
      builder: (BuildContext context, AsyncSnapshot snapshot) {
        switch (snapshot.connectionState) {
          case ConnectionState.none:
          case ConnectionState.waiting:
            return new CircularProgressIndicator();
          default:
            if (snapshot.hasError) {
              return new Text('Error: ${snapshot.error}');
            } else {
              print(snapshot.data);
              createListView(context, snapshot);
            }
        }
      },
    );
  }

  Widget createListView(BuildContext context, AsyncSnapshot snapshot) {
    List<BasicDiskInfo> disks = snapshot.data;
    return new ListView.builder(
      itemCount: disks.length,
      itemBuilder: (context, index) {
        return GestureDetector(
          onTap: () {
            Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => SpecificDiskPage(
                        diskId: disks[index].id,
                      ),
                ));
          },
          child: Card(
            child: Column(
              children: <Widget>[
                Row(
                  children: <Widget>[
                    Text(disks[index].name),
                    Spacer(),
                    Text(disks[index].driveType),
                    Spacer(),
                    Text(disks[index].driveFormat),
                  ],
                ),
                Row(
                  children: <Widget>[
                    Text(disks[index].totalSize.toString()),
                    Spacer(),
                    Text(disks[index].totalFreeSpace.toString()),
                  ],
                ),
              ],
            ),
          ),
        );
      },
    );
  }
}

BasicDiskInfo

class BasicDiskInfo {
  int id;
  String name;
  String driveType;
  String driveFormat;
  int totalSize;
  int totalFreeSpace;

  BasicDiskInfo(
      {this.id,
      this.name,
      this.driveType,
      this.driveFormat,
      this.totalSize,
      this.totalFreeSpace});

  factory BasicDiskInfo.fromJson(Map<String, dynamic> json) {
    return BasicDiskInfo(
        id: json['id'],
        name: json['name'],
        driveType: json['driveType'],
        driveFormat: json['driveFormat'],
        totalSize: json['totalSize'],
        totalFreeSpace: json['totalFreeSpace']);
  }
}

The FutureBuilder should return a list with the data from the api

1 Answers

2
nonybrighto On Best Solutions

There is an error in your build method. You didn't return createListView(context, snapshot); in your default case.