1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Flutter] Flutter bloc with streams

Discussão em 'Mobile' iniciado por Stack, Outubro 9, 2024 às 11:42.

  1. Stack

    Stack Membro Participativo

    I'm trying to use flutter bloc pattern in my application. It uses quick_blue library to scan BLE devices. QuickBlue provides a streams with scan results. My goal is to start scan with one event(emit new state each time device is found) and stop scan with another event.

    Problem: I am confused how to cancel an active scan event. Since scanResultStream comes from 3rd party library, I can't control it. So, emit.onEach stays active until another StartScanEvent fires (thanks to restartable()).

    I've looked in flutter bloc with stream example, but there stream is created inside application, so user can full control over it. I'd appreciate any advice.

    Here's my bloc code:

    class BLEBloc extends Bloc<BLEEvent, BLEState> {

    final List<BlueScanResult> scanResult = [];
    final Stream scanStream = QuickBlue.scanResultStream;

    BLEBloc() : super(BLEInitState()) {
    on<StartScanEvent>((event, emit) async {
    QuickBlue.startScan();
    scanResult.clear();

    // want to cancel this
    await emit.onEach(
    scanStream,
    onData: (data) {
    scanResult.add(data);
    emit(DeviceFoundState(scanResult));
    },
    );

    print("finished"); // finishes only on next StartScanEvent
    }, transformer: restartable());

    on<StopScanEvent>(
    (event, emit) {
    QuickBlue.stopScan();
    emit(ScanFinishedState());
    },
    );
    }
    }

    Continue reading...

Compartilhe esta Página