无法将抽象 class 的子类型作为参数传递给函数

Can't pass in a subtype of an abstract class as argument into a function

我按照BloC的教程做了,我有一个Bloc有这个方法

  @override Stream<FridgeState> mapEventToState(FridgeEvent event) async* { .. }

其中 FridgeEvent 是抽象的 class

abstract class FridgeEvent {
  const FridgeEvent();
}

class CreateFridgeEvent extends FridgeEvent {
  final double lat, lng;

  CreateFridgeEvent({required this.lat, required this.lng});
}

class DeleteFridgeEvent extends FridgeEvent {}

在小部件内实例化 bloc 并传入 FridgeEvent 的子 class 时,出现无法分配参数类型的错误。我做错了什么?

根据要求,这是集团的代码

import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:foodey_flutter/domain/fridge/entity/Fridge.dart';
import 'package:foodey_flutter/domain/fridge/usecase/CreateFridgeUseCase.dart';
import 'package:foodey_flutter/factory/FridgeFactory.dart';
import 'package:foodey_flutter/ui/fridge/create//event.dart';
import 'package:foodey_flutter/ui/fridge/create//state.dart';

class FridgeBloc extends Bloc<FridgeEvent, FridgeState> {
  CreateFridgeUseCase? createFridgeUseCase;

  FridgeBloc(FridgeState initialState) : super(initialState) {
    FridgeFactory.inject(this);
  }

  @override
  Stream<FridgeState> mapEventToState(FridgeEvent event) async* {
    if (event is CreateFridgeEvent) {
      Fridge? result = await createFridgeUseCase?.execute(event.lat, event.lng);
      if (result != null)
        yield FridgeSuccessfullyLoadedState(result);
      else
        yield FridgeErrorState(
            exception: Exception("Failed to create a fridge"));
    } else {
      yield FridgeErrorState(exception: Exception("Operation not supported"));
    }
  }
}

这是小部件的代码

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:foodey_flutter/ui/fridge/create/FridgeBloc.dart';
import 'package:foodey_flutter/ui/fridge/create/event.dart';

class CreateFridgeWidget extends StatefulWidget {
  @override
  _CreateFridgeState createState() => _CreateFridgeState();
}

class _CreateFridgeState extends State<CreateFridgeWidget> {
  late FridgeBloc _bloc;

  @override
  void initState() {
    super.initState();
    this._bloc = BlocProvider.of<FridgeBloc>(context);
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      key: widget.key,
      alignment: Alignment.center,
      padding: EdgeInsets.all(8),
      child: Column(
        children: [
          Text("Are you sure you want to create your fridge?"),
          Row(
            children: [
              TextButton(
                key: widget.key,
                onPressed: () {
                  _bloc.add(CreateFridgeEvent(lat: 0, lng: 1));
                },
                child: Text("Yes"),
              ),
              TextButton(
                key: widget.key,
                onPressed: () => {},
                child: Text("No"),
              ),
            ],
          )
        ],
      ),
    );
  }
}

这是我定义的事件

abstract class FridgeEvent {
  const FridgeEvent();
}

class CreateFridgeEvent extends FridgeEvent {
  final double lat, lng;

  CreateFridgeEvent({required this.lat, required this.lng});
}

class DeleteFridgeEvent extends FridgeEvent {}

更新:将代码复制粘贴到此处并检查完整性后,我发现了问题。对于面临相同问题的任何人:在 bloc 代码中导入的事件在路径中包含两个 /。删除一个 / 后,编译器不再抱怨传递了错误的类型。不知道第二个 / 是如何添加的,也不知道为什么编译器对此一点也不抱怨。显然,从 ../event.dart

导入时,..//event.dart 中定义的对象并不相同

将代码复制粘贴到此处并检查完整性后,我发现了问题。对于面临相同问题的任何人:在 bloc 代码中导入的事件在路径中包含两个 /。删除一个 / 后,编译器不再抱怨传递了错误的类型。不知道第二个 / 是如何添加的,也不知道为什么编译器对此一点也不抱怨。 显然,从 ../event.dart

导入时,..//event.dart 中定义的对象并不相同