我正在嘗試制作一個列出藍牙設備的應用程式。我在我的專案中使用flutter_blue_plus。
我如下掃描并將找到的設備添加到串列中。
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);
}
});
});
},
)
串列定義
late List<ScanResult> deviceList = [];
我將添加到串列中的資料轉換為如下圖所示的影像。即使將元素添加到串列中,我也無法查看串列。我不明白問題的根源。程式的任何地方都沒有錯誤。
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(),
),
),
),
)
你能幫我嗎 ?
uj5u.com熱心網友回復:
得到結果后嘗試做 setState ..
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);
}
});
});
},
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/508537.html
下一篇:我想在一個容器中只顯示4行資料,然后在第二個容器中顯示接下來的4行資料,并借助顫動中的串列ViewBuilder