Home > Net >  Listview show nothing with asynchronous method
Listview show nothing with asynchronous method

Time:01-12

I don't know why when I build my project, no error are return but my listview is empty..

The class :

    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}€")
            ],
          )
        ],
      );
    }

I have print my response.data?.length and it not empty. At first it gave me the error "has size" but now the debug console is empty too... You can find my project on GitLab : https://gitlab.com/victor.nardel/trash-project-flutter

Thank you in advance for your help

CodePudding user response:

the error is caused by ListView.builder

simple solution: wrap your ListView with Expanded

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

for better code: just return the Widget, not List of widget. something like below:

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

so you can avoid redundant Widget.

  • Related