gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 07:11:36 28 4
gpt4 key购买 nike

我有一个返回数据列表的 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)".

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

应用程序接口(interface)

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

关于api - 为什么我的 FutureBuilder 不返回任何数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56200436/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com