Home > database >  Can not read properties of null, ListView.Builder
Can not read properties of null, ListView.Builder

Time:11-10

I have this problem in flutter, I want to fetch API with listview builder but they show this error after 2 seconds i got my data

enter image description here

//listviewBuilder

  return Container(
    height: 380,
    child: ListView.builder(
        padding: EdgeInsets.symmetric(vertical: 8.0),
        scrollDirection: Axis.horizontal,
        itemCount: listtest.length,
        itemBuilder: (context, index) {
          return index == listtest[index]
              ? SizedBox(width: 10)
              : Card(
                  margin: EdgeInsets.only(right: 8.0),
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.all(Radius.circular(10.0))),
                  child: Container(
                    height: 400,
                    width: 370,
                    decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(10.0),
                    ),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        GestureDetector(
                          onTap: () {
                            Navigator.pushNamed(context, 'HouseDetails');
                          },
                          child: Container(
                            height: 260,
                            decoration: BoxDecoration(
                                image: DecorationImage(
                                    fit: BoxFit.fill,
                                    image: AssetImage(images[index])),
                                borderRadius: BorderRadius.only(
                                  topLeft: Radius.circular(10.0),
                                  topRight: Radius.circular(10.0),
                                )),
                          ),
                        ),
                        Container(
                          padding: EdgeInsets.all(8.0),
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text(
                                listtest[index]['name'].toString(),
                                style: kh4,
                              ),
                              SizedBox(
                                height: 2,
                              ),
                              Text(
                                addresses[index],
                                style:
                                    TextStyle(color: Colors.grey, fontSize: 10),
                                maxLines: 1,
                                overflow: TextOverflow.ellipsis,
                              ),
                              SizedBox(
                                height: 2,
                              ),
                              IntrinsicHeight(
                                child: Row(
                                  children: [
                                    Text(
                                      '3 ',
                                      style: TextStyle(
                                          fontWeight: FontWeight.bold,
                                          fontSize: 12),
                                    ),
                                    Text(
                                      'bds ',
                                      style: TextStyle(fontSize: 12),
                                    ),
                                    VerticalDivider(
                                      width: 1,
                                      color: Colors.grey,
                                    ),
                                    Text(
                                      ' 5 ',
                                      style: TextStyle(
                                          fontWeight: FontWeight.bold,
                                          fontSize: 12),
                                    ),
                                    Text(
                                      'baths ',
                                      style: TextStyle(fontSize: 12),
                                    ),
                                    VerticalDivider(
                                      width: 1,
                                      color: Colors.grey,
                                    ),
                                    Text(
                                      ' 1750 ',
                                      style: TextStyle(
                                          fontWeight: FontWeight.bold,
                                          fontSize: 12),
                                    ),
                                    Text(
                                      'sqft',
                                      style: TextStyle(fontSize: 12),
                                    ),
                                  ],
                                ),
                              ),
                              Row(
                                crossAxisAlignment: CrossAxisAlignment.center,
                                children: [
                                  Column(
                                    crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                    children: [
                                      SizedBox(
                                        height: 7,
                                      ),
                                      Text(
                                        'Starting Price',
                                        style: TextStyle(
                                          fontSize: 12,
                                        ),
                                      ),
                                      Text(
                                        prices[index],
                                        style: TextStyle(
                                            color: primaryColor,
                                            fontWeight: FontWeight.bold,
                                            fontSize: 11),
                                      )
                                    ],
                                  ),
                                  Spacer(),
                                  HeartContainer(index)
                                ],
                              )
                            ],
                          ),
                        )
                      ],
                    ),
                  ),
                );
        }),
  );

CodePudding user response:

It seems to me listtest can be null. You should make sure you take that into account.

Try replacing listtest.length with listtest?.length ?? 0

and

listtest[index] with listtest?.elementAt(index)

  • Related