Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter getter isn't specified for the class, when it is specified

Tags:

flutter

dart

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 ?

like image 584
syl-lo Avatar asked Nov 16 '18 14:11

syl-lo


1 Answers

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);
}
like image 134
Günter Zöchbauer Avatar answered Oct 20 '22 16:10

Günter Zöchbauer