我试图用我服务的蓝色人种使用河流。
当我开始发现我的所有装置时,我想在一份清单中显示我的所有装置。 根据这一法典,我的名单上没有发生:
我的意见:
class ModulesDiscoverView extends ConsumerWidget {
const ModulesDiscoverView({Key? key}) : super(key: key);
@override
Widget build(BuildContext context, WidgetRef ref) {
return Scaffold(
body: Column(
children: [
ElevatedButton(
onPressed: () => ref.read(bluetoothServiceProvider).discoverDevices(),
child: const Text("Discover devices"),
),
Expanded(
child: ListView.builder(
itemCount: ref.watch(bluetoothServiceProvider).modulesScanned.length,
itemBuilder: (context, index) {
final module = ref.watch(bluetoothServiceProvider).modulesScanned[index];
return Text(module.name);
},
),
),
],
),
);
}
}
模块:
class Module {
String name;
Module({required this.name});
}
Bluetooth Service.dart:
final bluetoothServiceProvider = Provider(BluetoothService.new);
class BluetoothService {
final Ref ref;
BluetoothService(this.ref);
StreamSubscription? subscriptionDiscoverDevices;
List<Module> modulesScanned = [];
FlutterReactiveBle flutterReactiveBle = FlutterReactiveBle();
Future<void> discoverDevices() async {
subscriptionDiscoverDevices = flutterReactiveBle.scanForDevices(withServices: [SERVICE_UUID]).listen((device) {
modulesScanned.add(Module(name: device.name));
}, onError: (error) {});
}
}