为什么我的FutureBuilder不返回任何数据?

时间:2019-05-18 15:37:04

标签: api flutter

我有一个api返回数据列表。 当我检索这些数据时,我使用FutureBuilder显示数据列表。 但是由于某种原因,即使我打印响应时也看不到我的数据。

这是我得到的错误:

══╡ 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)".

我不知道该怎么办。帮帮我吗?

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');
    }
  }

页面

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']);
  }
}

FutureBuilder应该返回一个列表,其中包含来自api的数据

1 个答案:

答案 0 :(得分:2)

您的生成方法有错误。在默认情况下,您没有返回createListView(context, snapshot);