为什么飞镖只想关闭我的一个集团?

Why dart wants to close only one of my blocs?

我有两个 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();
  }
}

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();
  }
}

然后我在屏幕中使用这些块:

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,
              ),
            ),
          );
        }
      },
    );
  }
}

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(
                        color: ColorHelper.white,
                        borderRadius: BorderRadius.circular(30.0),
                        border: Border.all(color: ColorHelper.darkBlue),
                      ),
                      child: GestureDetector(
                        onTap: () => _sensorsBloc.add(
                          UpdateSensorRequested(
                            sensorToBeUpdated: sensor,
                            isMap: true,
                          ),
                        ),
                        child: Container(
                          padding: const EdgeInsets.only(top: 4.0),
                          child: Text(
                            sensor.id.toString(),
                            textAlign: TextAlign.center,
                            style: Styles.darkBlueRegular12,
                          ),
                        ),
                      ),
                    ),
                  ),
                )
                .toList();
            return Stack(
              children: [
                MapWidget(markers: markers),
                Positioned(
                  left: 16,
                  bottom: 16,
                  child: CustomFloatingButton(
                    onPressed: () {
                      widget._setMap();
                      _sensorsBloc.add(SensorsRequested());
                    },
                    icon: Icon(
                      Icons.list_outlined,
                      color: ColorHelper.white,
                    ),
                    label: S.current.show_list,
                  ),
                ),
              ],
            );
          }
          return Center(
            child: Text('No sensor'),
          );
        }

        return Column(
          children: [
            Text(state.toString()),
            Center(
              child: RaisedButton.icon(
                onPressed: () => _sensorsBloc.add(SensorsRequested()),
                icon: Icon(Icons.refresh_outlined),
                label: Text('Refresh'),
              ),
            ),
          ],
        );
      },
      listener: (BuildContext context, SensorsState state) {
        if (state is SensorsError) {
          showDialog(
            context: context,
            builder: (context) => CustomPlatformAlertDialog(
              title: S.current.register_error_default,
              message: Text(
                state.message,
                style: Styles.defaultGreyRegular14,
              ),
            ),
          );
        }

        if (state is AddSensorInitial) {
          Navigator.push(
            context,
            MaterialPageRoute(
              builder: (context) => BlocProvider.value(
                value: _sensorsBloc,
                child: AddEditScreen(
                  isEdit: false,
                  canDelete: widget._userRights == 1,
                ),
              ),
            ),
          );
        }
        if (state is UpdateSensorInitial) {
          Navigator.push(
            context,
            MaterialPageRoute(
              builder: (context) => BlocProvider.value(
                value: _sensorsBloc,
                child: AddEditScreen(
                  isEdit: true,
                  canDelete: widget._userRights == 1,
                  sensor: state.sensorToBeUpdated,
                  isMap: state.isMap,
                ),
              ),
            ),
          );
        }
        if (state is SensorAdded) {
          Navigator.pop(context);
          showDialog(
            context: context,
            builder: (context) => CustomPlatformAlertDialog(
              title: 'Success',
              message: Text(
                'Sensor id: ${state.addedSensor.id}\nlatitude: ${state.addedSensor.latitude}\nlongitude: ${state.addedSensor.longitude}',
              ),
            ),
          );
        }
        if (state is SensorUpdated) {
          Navigator.pop(context);
          showDialog(
            context: context,
            builder: (context) => CustomPlatformAlertDialog(
              title: 'Success',
              message: Text(
                'Sensor id: ${state.updatedSensor.id}\nlatitude: ${state.updatedSensor.latitude}\nlongitude: ${state.updatedSensor.longitude}',
              ),
            ),
          );
        }

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

我遇到的问题是,在 _historicalEventsBloc 旁边的屏幕 HistoryScreen 中出现此错误:Close instances of dart.core.Sink.dartclose_sinks,但在 SensorsScreen 中没有这样的错误。

你知道为什么会这样吗? 我不想关闭我的流,因为我需要它们在使用应用程序时处于活动状态。

谢谢

您不必担心它会关闭您的流。它不会那样做。它只是一个警告,可以忽略。它出现的原因是因为您将 bloc 保存为变量。该变量将无法为您提供发出的状态。

为了避免这些,不要将 bloc 分配给变量。使用 context.read() 或 context.watch() 添加事件,关于差异的更多详细信息 here,您将不再遇到该问题。