gpt4 book ai didi

dart - Flutter getter 没有为类指定,当它被指定时

转载 作者:IT老高 更新时间:2023-10-28 12:40:40 26 4
gpt4 key购买 nike

我来了

[dart] The getter 'currentPickup' isn't defined for the class PickupEvent[undefined_getter]

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();
}
}

当在 PickupEvent 类中指定 getter 并在pickup.dart 中正确导入时。

import '../../../models/models.dart';

abstract class PickupEvent {}

class ButtonPressed extends PickupEvent {
final PickupModel currentPickup;

ButtonPressed({
@required this.currentPickup
});
}

知道是什么原因造成的吗?

最佳答案

错误信息是正确的。 ButtonPressed 有这样的 getter,但 PickupEvent 没有。

您可以向基类添加抽象 getter

abstract class PickupEvent {
PickupModel get currentPickup;
}

或投喜欢

_pushPickup((event as ButtonPressed).currentPickup);

如果您确定它将是 ButtonPressed

你也可以这样做

if(event is ButtonPressed) {
_pushPickup(event.currentPickup);
}

关于dart - Flutter getter 没有为类指定,当它被指定时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53340077/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com