flutter 使用异步方法时列表视图不显示任何内容

yc0p9oo0  于 2023-01-14  发布在  Flutter
关注(0)|答案(1)|浏览(191)

我不知道为什么当我构建我的项目时,没有返回任何错误,但我的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
提前感谢您的帮助

falq053o

falq053o1#

错误是由ListView.builder引起的
简单解决方案:用Expanded Package 您的ListView

if (response.hasData) {
       children = <Widget>[
          Expanded(
            child:ListView.builder(
       ....

更好的代码:只是返回小部件,而不是列表的小部件.如下所示:

if(hasdata) return Listview();
else if(has error) return Column();
else return CircularIndicator();

这样就可以避免多余小部件。

相关问题