1

I'm confused why yielding a new state is not causing the state variable inside by bloc class to actually update. The value of state remains what I initialize it to despite mapping events to to functions that are yielding new states. For example if my state had a count field and was initialized to 0. I would expect if I add an increment event to my bloc I would be able to produce the desired result by doing yield state.copyWith(count: state.count+1) but state.count is always 0 no matter what it never changes no matter how many times I send an increment envent. I was using freezed and swapped to using equatable for defining my state class to see if that would make a difference and it did not. When using a bloc does the state not update between yields ? What am I missing.

I should be seeing state.count increase in my print statement everytime _mapCameraOrientationCheckToState gets fired but state.count is always 0 which is

...Bloc Class Excerpt
  CameraBloc({
    this.resolutionPreset = ResolutionPreset.ultraHigh,
    this.cameraLensDirection = CameraLensDirection.back,
  }) : super(CameraState(
            error: null,
            path: null,
            orientation: null,
            count: 0,
            status: CameraStatus.initializing)) {
  }


  @override
  Stream<CameraState> mapEventToState(
    CameraEvent event,
  ) async* {
    if (event is CameraInitialize) {
      yield* _mapCameraInitializedToState(event);
    } else if (event is CameraCapture) {
      yield* _mapCameraCapturedToState(event);
    } else if (event is CameraStartRecording) {
      yield* _mapCameraRecordingToState(event);
    } else if (event is CameraStopRecording) {
      yield* _mapCameraStoppedToState(event);
    } else if (event is CheckCameraOrientation) {
      yield* _mapCameraOrientationCheckToState(event);
    }
  }

Stream<CameraState> _mapCameraOrientationCheckToState(
      CheckCameraOrientation event) async* {
    try {
      print(state.count);
      yield CameraState(
        error: null,
        path: null,
        count: state.count + 1,
        orientation: null,
        status: CameraStatus.capturing,
      );
      //}
    } catch (e) {
      print(e);
    }
  }


...

enum CameraStatus {
  initializing,
  recording,
  capturing,
  stopped,
  error,
  ready,
}

class CameraState extends Equatable {
  final Object? error;
  final String? orientation;
  final String? path;
  final CameraStatus status;
  final int count;

  const CameraState(
      {required this.error,
      required this.orientation,
      required this.path,
      required this.status,
      required this.count});

  CameraState copyWith(
      {Object? error,
      String? orientation,
      String? path,
      CameraStatus? status,
      int? count}) {
    return CameraState(
        error: error ?? this.error,
        orientation: orientation ?? this.orientation,
        path: path ?? this.path,
        status: status ?? this.status,
        count: count ?? this.count);
  }

  @override
  List<Object?> get props => [error, orientation, path, status, count];
}



abstract class CameraEvent extends Equatable {
  const CameraEvent();

  @override
  List<Object> get props => [];
}

class CameraInitialize extends CameraEvent {}

class CameraStopRecording extends CameraEvent {}

class CameraCapture extends CameraEvent {}

class CameraStartRecording extends CameraEvent {}

class CheckCameraOrientation extends CameraEvent {}

user2561417
  • 295
  • 4
  • 13

0 Answers0