api - 为什么我的 FutureBuilder 不返回任何数据?
问题描述
我有一个返回数据列表的 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()),
],
),
],
),
),
);
},
);
}
}
基本磁盘信息
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 的数据的列表
解决方案
您的构建方法中存在错误。您没有createListView(context, snapshot);
在默认情况下返回。
推荐阅读
- javascript - Jquery 无法识别我调用的函数
- android - Kotlin 中的同步单例
- ant - 构建 FreeMarker 不解决依赖关系
- lora - 我想在瑞萨 Synergy s7 板上为 lora 创建一个库
- python - 使用静态依赖项安装 lxml
- node.js - 在 $project 中将数组转换为字符串(聚合)
- racket - 如何让我的 Racket 设计的语言从外部文件运行脚本?
- c# - “LINQ to Entities 不支持指定的类型成员‘日期’”
- python - 如何在树莓派上为 python3.7 安装 opencv 3.4.6?
- swift - 当 SecureField 在 SwiftUI 中获得焦点/不焦点时执行代码