4ankoku
4ankoku

Reputation: 25

Error BLOC state [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: UnimplementedError

first of all, i got an error look like this

I/flutter ( 3245): BLOC Start
I/flutter ( 3245): BLOC Start

════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following NoSuchMethodError was thrown building BlocBuilder<StationBloc, StationState>(dirty, state: _BlocBuilderBaseState<StationBloc, StationState>#24ede):
The getter 'length' was called on null.
Receiver: null
Tried calling: length

The relevant error-causing widget was: 
  BlocBuilder<StationBloc, StationState> file:///C:/Users/junia/FlutterApp/airportstation/airport_station/lib/view/home_screen.dart:42:18
When the exception was thrown, this was the stack: 
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
#1      _HomeScreenState.buildStationList (package:airport_station/view/home_screen.dart:79:30)
#2      _HomeScreenState.build.<anonymous closure> (package:airport_station/view/home_screen.dart:51:24)
#3      BlocBuilder.build (package:flutter_bloc/src/bloc_builder.dart:90:57)
#4      _BlocBuilderBaseState.build (package:flutter_bloc/src/bloc_builder.dart:162:48)

Then, i went to stackoverflow and found a litte solution

Widget buildStationList(List<AllFlight> allFlight) {
    return ListView.builder(
        itemCount: allFlight.length,
        itemBuilder: (BuildContext context, int index) {
          return Padding(
            padding: const EdgeInsets.all(8.0),
            child: InkWell(
              child: ListTile(
                title: Text(allFlight[index].airportName + ' - ' + allFlight[index].countryName),
                subtitle: Text(allFlight[index].label),
              ),
            ),
          );
        }
    );

the problem is on itemCount: allFlight.length, and i change it into itemCount: allFlight?.length ?? 0,

after i run it again. there's still an error

I/flutter ( 3245): BLOC Start
I/flutter ( 3245): BLOC Start
E/flutter ( 3245): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: UnimplementedError
E/flutter ( 3245): #0      StationLoadedState.props (package:airport_station/bloc/station_state.dart:23:29)
E/flutter ( 3245): #1      Equatable.== (package:equatable/src/equatable.dart:50:18)
E/flutter ( 3245): #2      Bloc._bindEventsToStates.<anonymous closure> (package:bloc/src/bloc.dart:261:32)
E/flutter ( 3245): #3      _rootRunUnary (dart:async/zone.dart:1198:47)
E/flutter ( 3245): #4      _CustomZone.runUnary (dart:async/zone.dart:1100:19)
E/flutter ( 3245): #5      _CustomZone.runUnaryGuarded (dart:async/zone.dart:1005:7)
E/flutter ( 3245): #6      _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:357:11)
E/flutter ( 3245): #7      _BufferingStreamSubscription._add (dart:async/stream_impl.dart:285:7)
E/flutter ( 3245): #8      _SyncBroadcastStreamController._sendData (dart:async/broadcast_stream_controller.dart:385:25)
E/flutter ( 3245): #9      _BroadcastStreamController._add (dart:async/broadcast_stream_controller.dart:293:5)
E/flutter ( 3245): #10     _rootRunUnary (dart:async/zone.dart:1198:47)
E/flutter ( 3245): #11     _CustomZone.runUnary (dart:async/zone.dart:1100:19)
E/flutter ( 3245): #12     _CustomZone.runUnaryGuarded (dart:async/zone.dart:1005:7)
E/flutter ( 3245): #13     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:357:11)
E/flutter ( 3245): #14     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:285:7)
E/flutter ( 3245): #15     _ForwardingStreamSubscription._add (dart:async/stream_pipe.dart:127:11)
E/flutter ( 3245): #16     _MapStream._handleData (dart:async/stream_pipe.dart:224:10)
E/flutter ( 3245): #17     _ForwardingStreamSubscription._handleData (dart:async/stream_pipe.dart:157:13)
E/flutter ( 3245): #18     _rootRunUnary (dart:async/zone.dart:1198:47)
E/flutter ( 3245): #19     _CustomZone.runUnary (dart:async/zone.dart:1100:19)
E/flutter ( 3245): #20     _CustomZone.runUnaryGuarded (dart:async/zone.dart:1005:7)
E/flutter ( 3245): #21     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:357:11)
E/flutter ( 3245): #22     _DelayedData.perform (dart:async/stream_impl.dart:611:14)
E/flutter ( 3245): #23     _StreamImplEvents.handleNext (dart:async/stream_impl.dart:730:11)
E/flutter ( 3245): #24     _PendingEvents.schedule.<anonymous closure> (dart:async/stream_impl.dart:687:7)
E/flutter ( 3245): #25     _rootRun (dart:async/zone.dart:1182:47)
E/flutter ( 3245): #26     _CustomZone.run (dart:async/zone.dart:1093:19)
E/flutter ( 3245): #27     _CustomZone.runGuarded (dart:async/zone.dart:997:7)
E/flutter ( 3245): #28     _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1037:23)
E/flutter ( 3245): #29     _rootRun (dart:async/zone.dart:1190:13)
E/flutter ( 3245): #30     _CustomZone.run (dart:async/zone.dart:1093:19)
E/flutter ( 3245): #31     _CustomZone.runGuarded (dart:async/zone.dart:997:7)
E/flutter ( 3245): #32     _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1037:23)
E/flutter ( 3245): #33     _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
E/flutter ( 3245): #34     _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)
E/flutter ( 3245): 

my bloc_state.dart look like this

...

class StationLoadedState extends StationState {
  final List<AllFlight> allFlight;

  StationLoadedState({ @required this.allFlight });

  @override
  List<Object> get props => throw UnimplementedError();
}

...

and BlocBuilder

class _HomeScreenState extends State<HomeScreen> {
  StationBloc stationBloc;

  @override
  void initState() {
    super.initState();
    stationBloc = BlocProvider.of(context);
    stationBloc.add(FetchStationEvent());
  }

...

body: Container(
        child: BlocListener<StationBloc, StationState>(
          listener: (context, state) {
            if (state is StationErrorState) {
              print(state.message);
            }
          },
          child: BlocBuilder<StationBloc, StationState>(
            builder: (context, state) {
              print('BLOC Start');
              if (state is StationInitialState) {
                return buildLoading();
              } else if (state is StationLoadingState) {
                return buildLoading();
              } else if (state is StationLoadedState) {
                return buildStationList(state.allFlight);
              } else if (state is StationErrorState) {
                return buildError(state.message);
              }
            },
          ),
        ),
      ),
...

json response returned successfully when i try print it on http response. did someone know where the problem is?

Upvotes: 1

Views: 1513

Answers (1)

Thepeanut
Thepeanut

Reputation: 3397

BlocState has a "props" getter which should return an array of variables to compare states between each other. When you yield same state 2 times in a row, it will compare them using those props and if it's the same state as current - bloc will not yield it again (this is the default way it works when you're using Equatable).

In your code above you have this:

class StationLoadedState extends StationState {
  final List<AllFlight> allFlight;

  StationLoadedState({ @required this.allFlight });

  @override
  List<Object> get props => throw UnimplementedError();
}

It throws UnimplementedError when props are called. Change it to an array with your state's params.

class StationLoadedState extends StationState {
  final List<AllFlight> allFlight;

  StationLoadedState({ @required this.allFlight });

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

Upvotes: 1

Related Questions