Search code examples
flutterlistviewbluetooth

Even though I add an element to the listview, it is not refreshed


I'm trying to make an app that lists bluetooth devices. I am using flutter_blue_plus in my project.

I scan as follows and add the found devices to the list.

FloatingActionButton(
                backgroundColor: bleColor,
                child: Icon(
                  Icons.search,
                  color: Colors.white,
                ),
                onPressed: () {
                  setState(() {
                    flutterBlue.startScan(
                        timeout: const Duration(seconds: 4));
                    flutterBlue.scanResults.listen((results) {
                      for (ScanResult r in results) {
                        deviceList.add(r);
                      }
                    });
                  });
                },
              )

List define

late List<ScanResult> deviceList = [];

I make the data added to the list transform into an image like the one below. I can't view the list even though the element is added to the list. I did not understand the source of the problem. There is no error anywhere in the program.

Expanded(
                        child: Container(
                          child: Padding(
                            padding: const EdgeInsets.only(
                                left: 16, right: 16, top: 8, bottom: 8),
                            child: ListView(
                              children: deviceList
                                  .map((result) => Container(
                                        margin: EdgeInsets.symmetric(
                                            vertical: 8, horizontal: 16),
                                        decoration: BoxDecoration(
                                          color: Colors.white,
                                          borderRadius: BorderRadius.all(
                                              Radius.circular(20)),
                                          boxShadow: <BoxShadow>[
                                            BoxShadow(
                                              offset: Offset(4, 4),
                                              blurRadius: 10,
                                              color: Colors.grey
                                                  .withOpacity(.2),
                                            ),
                                            BoxShadow(
                                              offset: Offset(-3, 0),
                                              blurRadius: 15,
                                              color: Colors.grey
                                                  .withOpacity(.1),
                                            )
                                          ],
                                        ),
                                        child: Container(
                                          padding: EdgeInsets.symmetric(
                                              horizontal: 18, vertical: 8),
                                          child: ListTile(
                                              contentPadding:
                                                  EdgeInsets.all(0),
                                              leading: ClipRRect(
                                                borderRadius:
                                                    BorderRadius.all(
                                                        Radius.circular(
                                                            13)),
                                                child: Container(
                                                  height: 35,
                                                  width: 35,
                                                  decoration: BoxDecoration(
                                                    borderRadius:
                                                        BorderRadius
                                                            .circular(15),
                                                  ),
                                                  child:
                                                      Icon(Icons.bluetooth),
                                                ),
                                              ),
                                              title: result.device.name ==
                                                      ''
                                                  ? Text('Unknown',
                                                      style: TextStyle(
                                                          fontWeight:
                                                              FontWeight
                                                                  .bold))
                                                  : Text(result.device.name,
                                                      style: TextStyle(
                                                          fontWeight:
                                                              FontWeight
                                                                  .bold)),
                                              subtitle: Text(
                                                result.device.id.toString(),
                                                style: TextStyle(
                                                    fontWeight:
                                                        FontWeight.bold),
                                              ),
                                              trailing: TextButton(
                                                child: Text('Connect'),
                                                onPressed: () {},
                                              )),
                                        ),
                                      ))
                                  .toList(),
                            ),
                          ),
                        ),
                      )

Can you help me please ?


Solution

  • Try to do setState after getting results..

    FloatingActionButton(
                backgroundColor: bleColor,
                child: Icon(
                  Icons.search,
                  color: Colors.white,
                ),
                onPressed: () 
                    flutterBlue.startScan(
                        timeout: const Duration(seconds: 4));
                    flutterBlue.scanResults.listen((results) {
                      setState(() { // use here
                        for (ScanResult r in results) {
                          deviceList.add(r);
                        }
                      });
                  });
                },
              )