Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
147 views
in Technique[技术] by (71.8m points)

flutter - Why dart wants to close only one of my blocs?

I have two BLOCS.

imports ...
part 'historical_events_event.dart';

part 'historical_events_state.dart';

class HistoricalEventsBloc
    extends Bloc<HistoricalEventsEvent, HistoricalEventsState> {
  final HistoryRepository historyRepository;
  StreamSubscription _subscription;

  HistoricalEventsBloc({
    @required this.historyRepository,
  }) : super(HistoricalEventsLoading());

  @override
  Stream<HistoricalEventsState> mapEventToState(
    HistoricalEventsEvent event,
  ) async* {
    if (event is HistoricalEventsRequested) {
      yield HistoricalEventsLoading();
      try {
        await _subscription?.cancel();
        _subscription = historyRepository.oldEvents.listen(
          (QuerySnapshot _snapshot) => add(
            _HistoricalEventsLoaded(_snapshot.oldEventsFromSnapshot),
          ),
        );
      } catch (error) {
        yield HistoricalEventsError(message: error.toString());
      }
    }

    if (event is _HistoricalEventsLoaded) {
      yield HistoricalEventsLoaded(events: event.events);
    }

    if (event is DeleteHistoricalEvent) {
      yield HistoricalEventsLoading();
      try {
        final deleted = await historyRepository.deleteHistoricalEvent(
            eventId: event.eventToBeDeleted.id);
        if (deleted) {
          yield HistoricalEventsDeleted();
          await _subscription?.cancel();
          _subscription = historyRepository.oldEvents.listen(
            (QuerySnapshot _snapshot) => add(
              _HistoricalEventsLoaded(_snapshot.oldEventsFromSnapshot),
            ),
          );
        }
      } catch (error) {
        yield HistoricalEventsError(message: error.toString());
      }
    }

    if (event is HistoricalEventDetailRequested) {
      yield HistoricalEventsLoading();
      try {
        yield HistoricalEventDetail(sensors: event.sensors, event: event.event);
      } catch (error) {
        yield HistoricalEventsError(message: error.toString());
      }
    }
  }

  @override
  Future<void> close() {
    _subscription?.cancel();
    return super.close();
  }
}

and

imports ...

part 'sensors_event.dart';

part 'sensors_state.dart';

class SensorsBloc extends Bloc<SensorsEvent, SensorsState> {
  final SensorsRepository sensorsRepository;
  StreamSubscription _subscription;

  SensorsBloc({
    @required this.sensorsRepository,
  }) : super(SensorsLoading());

  @override
  Stream<SensorsState> mapEventToState(
    SensorsEvent event,
  ) async* {
    if (event is SensorsRequested) {
      yield SensorsLoading();
      try {
        await _subscription?.cancel();
        _subscription = sensorsRepository.sensors.listen(
          (QuerySnapshot _snapshot) => add(
            _SensorsLoaded(_snapshot.sensorsFromSnapshot),
          ),
        );
      } catch (error) {
        yield SensorsError(message: error.toString());
      }
    }

    if (event is _SensorsLoaded) {
      yield SensorsLoaded(sensors: event.sensors);
    }

    if (event is SensorsMapRequested) {
      yield SensorsLoading();
      try {
        await _subscription?.cancel();
        _subscription = sensorsRepository.sensors.listen(
          (QuerySnapshot _snapshot) => add(
            _SensorsMapLoaded(_snapshot.sensorsFromSnapshot),
          ),
        );
      } catch (error) {
        yield SensorsError(message: error.toString());
      }
    }

    if (event is _SensorsMapLoaded) {
      yield SensorsMapLoaded(sensors: event.sensors);
    }

    if (event is AddSensorRequested) {
      yield SensorsLoading();
      try {
        yield AddSensorInitial();
      } catch (error) {
        yield SensorsError(message: error.toString());
      }
    }

    if (event is AddSensor) {
      yield SensorsLoading();
      try {
        final added = await sensorsRepository.addSensor(
          id: event.id,
          latitude: event.latitude,
          longitude: event.longitude,
          address: event.address,
        );
        if (added) {
          final Sensor addedSensor = await sensorsRepository.findSensorById(
            id: event.id,
          );
          yield SensorAdded(addedSensor: addedSensor);
          await _subscription?.cancel();
          _subscription = sensorsRepository.sensors.listen(
            (QuerySnapshot _snapshot) => add(
              _SensorsLoaded(_snapshot.sensorsFromSnapshot),
            ),
          );
        }
      } catch (error) {
        yield SensorsError(message: error.toString());
      }
    }
    if (event is UpdateSensorRequested) {
      yield SensorsLoading();
      try {
        yield UpdateSensorInitial(
          sensorToBeUpdated: event.sensorToBeUpdated,
          isMap: event.isMap,
        );
      } catch (error) {
        yield SensorsError(message: error.toString());
      }
    }

    if (event is UpdateSensor) {
      yield SensorsLoading();
      try {
        final updated = await sensorsRepository.updateSensor(
          oldSensor: event.oldSensor,
          id: event.id,
          latitude: event.latitude,
          longitude: event.longitude,
          address: event.address,
        );

        if (updated) {
          final Sensor updatedSensor = await sensorsRepository.findSensorById(
            id: event.id,
          );

          yield SensorUpdated(updatedSensor: updatedSensor);
          await _subscription?.cancel();
          _subscription = sensorsRepository.sensors.listen(
            (QuerySnapshot _snapshot) => add(
              _SensorsLoaded(_snapshot.sensorsFromSnapshot),
            ),
          );
        }
      } catch (error) {
        yield SensorsError(message: error.toString());
      }
    }

    if (event is DeleteSensor) {
      yield SensorsLoading();

      try {
        final deleted = await sensorsRepository.deleteSensor(
            sensorDbId: event.sensorToBeDeleted.dbId);

        if (deleted) {
          yield SensorDeleted();
          await _subscription?.cancel();
          _subscription = sensorsRepository.sensors.listen(
            (QuerySnapshot _snapshot) => add(
              _SensorsLoaded(_snapshot.sensorsFromSnapshot),
            ),
          );
        }
      } catch (error) {
        yield SensorsError(message: error.toString());
      }
    }
  }

  @override
  Future<void> close() {
    _subscription?.cancel();
    return super.close();
  }
}

Then I am using these blocs in Screens:

class HistoryScreen extends StatefulWidget {
  final int _userRights;

  const HistoryScreen({
    Key key,
    @required int userRights,
  })  : this._userRights = userRights,
        super(key: key);

  @override
  _HistoryScreenState createState() => _HistoryScreenState();
}

class _HistoryScreenState extends State<HistoryScreen> {
  HistoricalEventsBloc _historicalEventsBloc;

  @override
  void initState() {
    super.initState();
    _historicalEventsBloc = BlocProvider.of<HistoricalEventsBloc>(context);
  }

  @override
  Widget build(BuildContext context) {
    return BlocConsumer<HistoricalEventsBloc, HistoricalEventsState>(
      builder: (BuildContext context, HistoricalEventsState state) {
        if (state is HistoricalEventsLoaded) {
          if (state.events.isNotEmpty) {
            return Container(
              height: MediaQuery.of(context).size.height,
              child: Stack(
                children: [
                  SingleChildScrollView(
                    child: HistoryList(historicalEvents: state.events),
                  ),
                ],
              ),
            );
          }
          return Center(
            child: Text('No event in history'),
          );
        }
        return Center(child: CustomCircularIndicator());
      },
      listener: (BuildContext context, HistoricalEventsState state) {
        if (state is HistoricalEventsError) {
          showDialog(
            context: context,
            builder: (context) => CustomPlatformAlertDialog(
              title: S.current.register_error_default,
              message: Text(
                state.message,
                style: Styles.defaultGreyRegular14,
              ),
            ),
          );
        }

        if (state is HistoricalEventsDeleted) {
          showDialog(
            context: context,
            builder: (context) => CustomPlatformAlertDialog(
              title: 'Success',
              message: Text('Event deleted'),
            ),
          );
        }

        if (state is HistoricalEventDetail) {
          Navigator.push(
            context,
            MaterialPageRoute(
              builder: (context) => BlocProvider.value(
                child: HistoryDetailScreen(
                  canDelete: widget._userRights == 1,
                  event: state.event,
                  sensors: state.sensors,
                ),
                value: _historicalEventsBloc,
              ),
            ),
          );
        }
      },
    );
  }
}

and

class SensorsScreen extends StatefulWidget {
  final int _userRights;
  final Function _setMap;

  const SensorsScreen({
    Key key,
    @required int userRights,
    @required Function setMap,
  })  : this._userRights = userRights,
        this._setMap = setMap,
        super(key: key);

  @override
  _SensorsScreenState createState() => _SensorsScreenState();
}

class _SensorsScreenState extends State<SensorsScreen> {
  SensorsBloc _sensorsBloc;

  @override
  void initState() {
    super.initState();
    _sensorsBloc = BlocProvider.of<SensorsBloc>(context);
  }

  @override
  Widget build(BuildContext context) {
    return BlocConsumer<SensorsBloc, SensorsState>(
      builder: (BuildContext context, SensorsState state) {
        if (state is SensorsLoading ||
            state is AddSensorInitial ||
            state is UpdateSensorInitial) {
          return Center(child: CustomCircularIndicator());
        }

        if (state is SensorsLoaded) {
          if (state.sensors.isNotEmpty) {
            return Container(
              height: MediaQuery.of(context).size.height,
              child: Stack(
                children: [
                  SingleChildScrollView(
                    child: SensorsList(sensors: state.sensors),
                  ),
                  Positioned(
                    left: 16,
                    bottom: 16,
                    child: CustomFloatingButton(
                      onPressed: () {
                        widget._setMap();
                        _sensorsBloc.add(SensorsMapRequested());
                      },
                      icon: Icon(
                        Icons.map_outlined,
                        color: ColorHelper.white,
                      ),
                      label: S.current.show_on_map,
                    ),
                  ),
                ],
              ),
            );
          }
          return Center(
            child: Text('No sensor'),
          );
        }

        if (state is SensorsMapLoaded) {
          if (state.sensors.isNotEmpty) {
            List<Marker> markers = state.sensors
                .map(
                  (Sensor sensor) => Marker(
                    width: 25.0,
                    height: 25.0,
                    point: LatLng(sensor.latitude, sensor.longitude),
                    builder: (ctx) => Container(
                      decoration: BoxDecoration(
                

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You don't need to worry about it closing your stream. It won't do that. Its just a warning and can be disregarded. The reason it shows up is because you are saving your bloc as a variable. And that variable will not be able to provide you the states that are emitted.

To avoid these just don't assign the bloc to a variable. Add events with context.read() or context.watch(), more details about the difference here, and you will stop having that problem.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...