我不知道为什么当我构建我的项目时,没有返回任何错误,但我的listview
为空。
课程:
final LocationService service = LocationService();
late Future<List<Location>> _locations;
@override
void initState() {
super.initState();
_locations = service.getLocations();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Mes locations'),
),
bottomNavigationBar: const BottomNavBar(2),
body: Center(
child: FutureBuilder<List<Location>>(
future: _locations,
builder:
(BuildContext context, AsyncSnapshot<List<Location>> response) {
List<Widget> children;
if (response.hasData) {
children = <Widget>[
ListView.builder(
itemCount: response.data?.length,
itemBuilder: (context, index) =>
_BuildRow(response.data?[index]),
itemExtent: 285,
),
];
} else if (response.hasError) {
children = <Widget>[
const Icon(
Icons.error_outline,
color: Colors.red,
size: 40,
),
const Padding(
padding: EdgeInsets.only(top: 16),
child: Text('Un problème est survenu'),
),
];
} else {
children = const <Widget>[
SizedBox(
width: 50,
height: 50,
child: CircularProgressIndicator(
strokeWidth: 6,
),
),
];
}
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: children,
),
);
}),
),
);
}
// ignore: non_constant_identifier_names
_BuildRow(Location? location) {
return Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Column(
children: [
Text(
"OKOK",
style: LocationTextStyle.priceBoldGreyStyle,
),
Text("${location?.dateFin}")
],
),
Text("${location?.montanttotal}€")
],
)
],
);
}
我打印了我的response.data?.length
,它不是空的。一开始它给了我错误“有大小”,但现在调试控制台也是空的...你可以在GitLab上找到我的项目:https://gitlab.com/victor.nardel/trash-project-flutter
提前感谢您的帮助
1条答案
按热度按时间falq053o1#
错误是由
ListView.builder
引起的简单解决方案:用
Expanded
Package 您的ListView
更好的代码:只是返回小部件,而不是列表的小部件.如下所示:
这样就可以避免多余小部件。