Undefined name 'state' for flutter bloc version 8.0.0

Issue

I am trying to access bloc (using newest version of flutter_bloc: 8.0.0) state value within BlocBuilder, but I keep getting error that state is undefined. I found this answer to similar question, but that’s not the issue. SetInitialValuesEvent is added when ConvertBloc is initialised and _onSetInitialValuesEventToState() is being called as well.

Only thing I could think of to try was checking if state is defined (accessible) in BlocConsumer listener (and it is):

Widget build(BuildContext context) {
        return BlocConsumer<ConvertBloc, ConvertState>(
          listener: (context, state) {
          //defined / accessible
          print("------- ${state.unitSystemFrom}");
          },
          builder: (context, sate) {
          //undefined / not accessible
            return Text(state.unitSystemFrom);
          },
        );
      }

Widget build method:

Widget build(BuildContext context) {
    return BlocBuilder<ConvertBloc, ConvertState>(
      builder: (context, sate) {
        return Text(state.unitSystemFrom);
      },
    );
  }

Set initial values event from bloc:

class SetInitialValuesEvent extends ConvertEvent {
  final String unitSystemFrom;
  final double unitValueFrom;
  final String unitFrom;
  final String unitSystemTo;
  final double unitValueTo;
  final String unitTo;

  const SetInitialValuesEvent(
    this.unitSystemFrom,
    this.unitValueFrom,
    this.unitFrom,
    this.unitSystemTo,
    this.unitValueTo,
    this.unitTo,
  );

  @override
  List<Object?> get props => [
        unitSystemFrom,
        unitValueFrom,
        unitFrom,
        unitSystemTo,
        unitValueTo,
        unitTo
      ];
}

Bloc:

class ConvertBloc extends Bloc<ConvertEvent, ConvertState> {
  ConvertBloc()
      : super(const ConvertState(
            unitSystemFrom: '',
            unitValueFrom: 0,
            unitFrom: '',
            unitSystemTo: '',
            unitValueTo: 0,
            unitTo: '')) {
    on<SetInitialValuesEvent>(_onSetInitialValuesEventToState);
  }

  void _onSetInitialValuesEventToState(
      SetInitialValuesEvent event, Emitter<ConvertState> emit) {
    emit(ConvertState(unitSystemFrom: event.unitSystemFrom,
        unitValueFrom: event.unitValueFrom,
        unitFrom: event.unitFrom,
        unitSystemTo: event.unitSystemTo,
        unitValueTo: event.unitValueTo,
        unitTo: event.unitTo))
  }
}

Solution

It seems like you have a Typo:

You used sate in the build function params but tried accessing it with state

Like this:

Widget build(BuildContext context) {
    return BlocBuilder<ConvertBloc, ConvertState>(
      builder: (context, sate) {
        return Text(state.unitSystemFrom);
      },
    );
  }

Use this instead:

Widget build(BuildContext context) {
    return BlocBuilder<ConvertBloc, ConvertState>(
      builder: (context, state) {
        return Text(state.unitSystemFrom);
      },
    );
  }

Answered By – Josteve

Answer Checked By – David Goodson (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.