I'm getting
[dart] The getter 'currentPickup' isn't defined for the class PickupEvent[undefined_getter]
error on event.currentPickup:
import 'dart:async';
import 'package:bloc/bloc.dart';
import 'pickup.dart';
import '../../../models/models.dart';
class PickupBloc extends Bloc<PickupEvent, PickupState> {
@override
Stream<PickupState> mapEventToState(PickupState state, PickupEvent event) async* {
if (state.isNotRunning) {
stopwatch.start();
timer = new Timer.periodic(new Duration(milliseconds: 200), updateTime);
yield PickupState.running();
}
if (state.isRunning) {
stopwatch.stop();
timer.cancel();
_pushPickup(event.currentPickup);
yield PickupState.done();
}
}
When the getter is specified in the PickupEvent class and properly imported in pickup.dart.
import '../../../models/models.dart';
abstract class PickupEvent {}
class ButtonPressed extends PickupEvent {
final PickupModel currentPickup;
ButtonPressed({
@required this.currentPickup
});
}
Any idea what could cause this ?
The error message is correct. ButtonPressed
has such a getter, but PickupEvent
does not.
You can either add an abstract getter to the base class
abstract class PickupEvent {
PickupModel get currentPickup;
}
or cast like
_pushPickup((event as ButtonPressed).currentPickup);
if you are sure it will be a ButtonPressed
.
You can also do
if(event is ButtonPressed) {
_pushPickup(event.currentPickup);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With