gpt4 book ai didi

flutter - 不能将抽象类的子类型作为参数传递给函数

转载 作者:行者123 更新时间:2023-12-05 04:51:25 25 4
gpt4 key购买 nike

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

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

其中 FridgeEvent 是一个抽象类

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 并传入 Frid​​geEvent 的子类时,出现无法分配参数类型的错误。我做错了什么?

enter image description here

根据要求,这里是 Bloc 的代码

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 中定义的对象并不相同

关于flutter - 不能将抽象类的子类型作为参数传递给函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66918372/

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