TheWCKD/blocFromZeroToHero

Always throws UnimplementedError

JakubChromiak opened this issue · 2 comments

With current Dart (2.12.2 - I don't know how it worked in the past) this line is always called as break only jumps out of switch, not a method. Because of that, there is always an error returned and printed into the console:

D:\SDK\Flutter\flutter\bin\cache\dart-sdk\bin\dart.exe --enable-asserts D:\Projects\Tutorials\Flutter\blockhero\tryout\lib\main-bloc.dart
lib/main-bloc.dart: Warning: Interpreting this as package URI, 'package:streams/main-bloc.dart'.
1
Unhandled exception:
Unhandled error UnimplementedError occurred in Instance of 'CounterBloc'.
#0 CounterBloc.mapEventToState (package:streams/main-bloc.dart:18:5)

#0 BlocBase.onError. (package:bloc/src/bloc.dart:389:7)
#1 BlocBase.onError (package:bloc/src/bloc.dart:390:6)
#2 _RootZone.runBinaryGuarded (dart:async/zone.dart:1558:10)
#3 _BufferingStreamSubscription._sendError.sendError (dart:async/stream_impl.dart:360:15)
#4 _BufferingStreamSubscription._sendError (dart:async/stream_impl.dart:378:16)
#5 _BufferingStreamSubscription._addError (dart:async/stream_impl.dart:280:7)
#6 _SyncBroadcastStreamController._sendError. (dart:async/broadcast_stream_controller.dart:393:20)
#7 _BroadcastStreamController._forEachListener (dart:async/broadcast_stream_controller.dart:323:15)
#8 _SyncBroadcastStreamController._sendError (dart:async/broadcast_stream_controller.dart:392:5)
#9 _BroadcastStreamController._addError (dart:async/broadcast_stream_controller.dart:290:5)
#10 _RootZone.runBinaryGuarded (dart:async/zone.dart:1558:10)
#11 _BufferingStreamSubscription._sendError.sendError (dart:async/stream_impl.dart:360:15)
#12 _BufferingStreamSubscription._sendError (dart:async/stream_impl.dart:378:16)
#13 _BufferingStreamSubscription._addError (dart:async/stream_impl.dart:280:7)
#14 _ForwardingStreamSubscription._addError (dart:async/stream_pipe.dart:128:11)
#15 _ForwardingStream._handleError (dart:async/stream_pipe.dart:95:10)
#16 _ForwardingStreamSubscription._handleError (dart:async/stream_pipe.dart:157:13)
#17 _RootZone.runBinaryGuarded (dart:async/zone.dart:1558:10)
#18 _BufferingStreamSubscription._sendError.sendError (dart:async/stream_impl.dart:360:15)
#19 _BufferingStreamSubscription._sendError (dart:async/stream_impl.dart:378:16)
#20 _DelayedError.perform (dart:async/stream_impl.dart:602:14)
#21 _StreamImplEvents.handleNext (dart:async/stream_impl.dart:706:11)
#22 _PendingEvents.schedule. (dart:async/stream_impl.dart:663:7)
#23 _microtaskLoop (dart:async/schedule_microtask.dart:40:21)
#24 _startMicrotaskLoop (dart:async/schedule_microtask.dart:49:5)
#25 _runPendingImmediateCallback (dart:isolate-patch/isolate_patch.dart:120:13)
#26 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:185:5)

Process finished with exit code 255

I faced the same issue. Just remove throw UnimplementedError();

Try the following code in the main-bloc.dart file:

@override
  Stream<int> mapEventToState(CounterEvent event) async* {
    try {
      switch (event) {
        case CounterEvent.increment:
          yield state + 1;
          break;
        case CounterEvent.decrement:
          yield state - 1;
          break;
      }
    } catch (error) {
      throw UnimplementedError();
    }
  }

You can check if the UnimplementedError() will be thrown by attempting a divide by zero calculation for one of the cases. The error will be thrown, thereby showing that the try...catch block works.