Avrilla Wardani
Avrilla Wardani

Reputation: 37

How to Fix "The getter 'length' was called on null

I already used Future Builder but still have this problem

This is my body code, i've already using future builder but still it have length problem. I dont understand this.

FutureBuilder(
                future: showPosts(),
                builder: (context, snapshot) {
                  return ListView.builder(
                      itemCount: snapshot.data.length,
                      itemBuilder: (context, index) {
                        return Card(
                          color: Color(0xFFE1F5FE),
                          child: Column(
                            mainAxisSize: MainAxisSize.min,
                            children: <Widget>[
                              const ListTile(
                                leading: Image(
                                    image: 
AssetImage('images/pantai.jpg')),
                                title: Text('Pantai Nglambor'),
                                subtitle: Text('by Ahmad Thariq Syauqi'),
                              ),
                              ButtonTheme.bar(
                                child: ButtonBar(
                                  children: <Widget>[
                                    FlatButton(
                                      child: const Text('DETAILS'),
                                      onPressed: () {
                                        Navigator.push(
                                          context,
                                          MaterialPageRoute(builder: 
(context) {
                                            return DetailPost();
                                          }),
                                        );
                                      },
                                    ),
                                  ],
                                ),
                              ),
                            ],
                          ),
                        );
                      });
                })

How can i fix this?

Upvotes: 0

Views: 94

Answers (1)

ejabu
ejabu

Reputation: 3147

as the Future builder class will run twice, initially snapshot value was null.

so the solution is we need to put conditional if.


if (snapshot.hasData)

Upvotes: 1

Related Questions