0

I've created a flutter project with FlutterBuilder with ListView.builder which get the data from JSON file (local). I successfully build the app, but just for a second the screen shown like this :

my error

after that, my app work smoothly. Here is my code :

FutureBuilder(
                future: _isInit ? fetchDoa(context) : Future(null),
                builder: (context, _) {
                  if (doaList.isNotEmpty) {
                    return ListView.builder(
                      itemCount: doaList.length,
                      itemBuilder: (BuildContext context, int index) {
                        Doa doa = doaList[index];
                        return Card(
                            margin: EdgeInsets.all(8),
                            child: ListTile(
                                title: Text(doa.judul),
                                onTap: () {
                                  Navigator.of(context).push(MaterialPageRoute(
                                      builder: (BuildContext context) =>
                                          DoaPage(
                                            doa: doa,
                                          )));
                                },
                                trailing: IconButton(
                                  icon: Icon(
                                    doa.fav
                                        ? Icons.favorite
                                        : Icons.favorite_border,
                                    color: doa.fav ? Colors.red : null,
                                  ),
                                  onPressed: () => setState(() {
                                    doa.fav = !doa.fav;
                                  }),
                                )));
                      },
                    );
                  }
                  return CircularProgressIndicator();
                })

This is the complete preview of my app :

enter image description here

I need your help, Thank you very much :)

Bill Rei
  • 333
  • 3
  • 14

4 Answers4

1

Change this:

if (doaList.isNotEmpty)

into this:

if (doaList != null)
ambiguous58
  • 1,241
  • 1
  • 9
  • 18
1

It's because you have a null data in the following line:

if (doaList.isNotEmpty)

and your FutureBuilder is slightly incorrect.

You need to update your FutureBuilder to something like this:

  // Change YourDoaList to your model.
  FutureBuilder<YourDoaList>(
    future: _isInit ? fetchDoa(context) : Future(null),
    builder: (BuildContext context, AsyncSnapshot<YourDoaList> snapshot) {
      Widget widget;
      if (snapshot.hasData) {
        // You have succesfully get the data.
        // check if you have correct data here
        YourDoaList items = snapshot.data;

        widget = ListView.builder(
           ...
        );
      } else if (snapshot.hasError) {
        // You've failed get the data
        widget = Text("Failed");
      } else {
        // waiting for data
        widget = CircularProgressIndicator();
      }

      return widget;
    },
  ),

See documentation here: https://api.flutter.dev/flutter/widgets/FutureBuilder-class.html

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
1

Make builder as

builder: (context, snapshot) {
 if(snapshot.hasData){
    if (doaList != null && doaList.isNotEmpty){
    // UI
    } else {
    // no data available
  }

 } else {
   // show loader or empty container
 }
Priyesh
  • 1,266
  • 5
  • 17
1

Though there are many good answer. But going by your present code. Just add {} where you defined your DAO list.

Like

var daoList = [];

And one important thing from official docs Link

The future must have been obtained earlier, e.g. during State.initState, State.didUpdateWidget, or State.didChangeDependencies. It must not be created during the State.build or StatelessWidget.build method call when constructing the FutureBuilder. If the future is created at the same time as the FutureBuilder, then every time the FutureBuilder's parent is rebuilt, the asynchronous task will be restarted.

So in your initState , do

@override
  void initState() {
    myFuture = getFuture();
    super.initState();
  }

and then in futureBuilder.

FutureBuilder(
    future: myFuture,
    remaining code
........
                
Nikhil Badyal
  • 1,589
  • 1
  • 9
  • 20