11

I am trying to display items in a ListView using ListView.builder inside a FutureBuilder. My future function for FutureBuilder looks like this:

_fetchListItems() async {
   wait() async {
     number = await db.getNumber(userId); }

   await wait();
 List rawFavouriteList = await db.getList(number);

  setState((){
  rawFavouriteList.forEach((item){
       _faouriteList.add(Model.map(item));
           }});
return _faouriteList;
}

My FutureBuilder looks like this:

            FutureBuilder(
               future: _fetchListItems(),
               builder:(context, AsyncSnapshot snapshot) {
                     if (!snapshot.hasData) {
                   return Center(child: CircularProgressIndicator());
         } else {Container( child: ListView.builder(                                                  
                itemCount: _faouriteList.length,
                scrollDirection: Axis.horizontal,
             itemBuilder: (BuildContext context, int index) {
                  return Text(                                                 
                   '${_faouriteList[index].title}');
              }));}})

he following assertion was thrown building FutureBuilder(dirty, state: I/flutter (24728): _FutureBuilderState#f12a3): I/flutter (24728): A build function returned null. I/flutter (24728): The offending widget is: FutureBuilder I/flutter (24728): Build functions must never return null

Another exception was thrown: A build function returned null.

Note:

I tried to call _fetchListItems() from initState and not use FutureBuilder and that didn't work for me as well.

Here is a link to that case: (Flutter/Dart) Two async methods in initState not working

Please let me know if I should use FutureBuilder or initState to wait for List to load it's data. And how to make it work since none of the methods seem to work for me :(

2
  • You can show your function _bowlerIconFetch() ? Commented Jun 20, 2019 at 22:54
  • @EdHuamani It was a typo. Its _fetchListItems() only. There is no _bowlerIconFetch. I have updated the code. Commented Jun 20, 2019 at 23:06

2 Answers 2

16

Your fetch _fetchListItems method is not the problem as far as I can tell.

Your error message is very clear, " a build function returned null". In this case, the method that returns null is the anonymous function passed as the builder argument to the FutureBuilder. You're just missing the return keyword inside the else case because you're just creating the instance of Container but not returning it.

Something like this:

FutureBuilder(
    future: _fetchListItems(),
    builder:(context, AsyncSnapshot snapshot) {
        if (!snapshot.hasData) {
            return Center(child: CircularProgressIndicator());
         } else {
            return Container(
                child: ListView.builder(                                                  
                    itemCount: _faouriteList.length,
                    scrollDirection: Axis.horizontal,
                    itemBuilder: (BuildContext context, int index) {
                        return Text('${_faouriteList[index].title}');                                           
                    }
                )
            );
         }
     }
)

I don't know if there are any other problems with your code but this should solve the particular error message you are asking about.

Sign up to request clarification or add additional context in comments.

2 Comments

This was such a silly mistake. I feel embarrassed. I would have gone haywire and wasn't going to find it anytime soon if you didn't point it out. Thanks :)
No problem! happens to all of us.
9

It is not necessary to use setState for your case, try this:

Fetch async function

  _fetchListItems() async {
    number = await db.getNumber(userId);
    List rawFavouriteList = await db.getList(number);
    List _faouriteList = rawFavouriteList.map((item)=>Model.map(item)).toList();
    return _faouriteList;
  }

FutureBuilder

FutureBuilder(
  future: _fetchListItems(),
  builder: (context, AsyncSnapshot snapshot) {
    if (!snapshot.hasData) {
      return Center(child: CircularProgressIndicator());
    } else {
      Container(
          child: ListView.builder(
              itemCount: snapshot.data.length,
              scrollDirection: Axis.horizontal,
              itemBuilder: (BuildContext context, int index) {
                return Text('${snapshot.data[index].title}');
              }));
    }
  });

3 Comments

I have removed setState
I wonder if something is wrong with using rawFavouriteList.forEach((item){ _faouriteList.add(Model.map(item)); }); that you have suggested instead _faouriteList = rawFavouriteList.map((item)=>Model.map(item)).toList();
Also please consider answering this question: stackoverflow.com/questions/56715327/… I believe you have good understanding of this based on your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.