gpt4 book ai didi

flutter - 'ImagePickerCubit' 不符合类型参数 'BlocBase' 的绑定(bind) 'B'

转载 作者:行者123 更新时间:2023-12-05 02:39:54 24 4
gpt4 key购买 nike

我在尝试在我的应用程序中创建 BlocBuilder 时遇到错误:“'ImagePickerCubit' 不符合类型参数 'B' 的绑定(bind) 'BlocBase'。尝试使用属于或属于 'BlocBase' 的子类的类型。”

我是按照文档实现的,所以我不知道是什么原因导致了这个错误。

这里是 cubit、state 和 widgets 的代码:

ImagePickerCubit:

part 'image_picker_state.dart';

class ImagePickerCubit extends Cubit {
final ImagePickerRepository _imagePickerRepository;

ImagePickerCubit(this._imagePickerRepository)
: super(ImagePickerInitialize());

Future<void> takePhotoFromGallery() async {
XFile? image;
try {
image = await _imagePickerRepository.takePhotoFromGallery();
emit(ImagePickerSuccess(image));
} catch (e) {
emit(ImagePickerFailure('Error while picking image: ${e.toString()}'));
log('Error while picking image from gallery: ${e.toString()}');
}
}

Future<void> takePhotoFromCamera() async {
XFile? image;
try {
image = await _imagePickerRepository.takePhotoFromCamera();
emit(ImagePickerSuccess(image));
} catch (e) {
emit(ImagePickerFailure('Error while picking image: ${e.toString()}'));
log('Error while picking image from camera: ${e.toString()}');
}
}
}

ImagePicker状态:

part of 'image_picker_cubit.dart';


class ImagePickerState extends Equatable {

@override
List<Object?> get props => [];
}

class ImagePickerInitialize extends ImagePickerState {}

class ImagePickerSuccess extends ImagePickerState {
final XFile? image;
ImagePickerSuccess(this.image);

@override
List<Object?> get props => [image];
}

class ImagePickerFailure extends ImagePickerState {
final String errorMessage;
ImagePickerFailure(this.errorMessage);

@override
List<Object?> get props => [errorMessage];
}

小部件:

class HomePageBuilder extends StatefulWidget {
const HomePageBuilder({
Key? key,
}) : super(key: key);

@override
State<HomePageBuilder> createState() => _HomePageBuilderState();
}

class _HomePageBuilderState extends State<HomePageBuilder> {
XFile? _image;

@override
Widget build(BuildContext context) {
ImagePickerCubit cubit =
ImagePickerCubit(RepositoryProvider.of<ImagePickerRepository>(context));

return Column(
children: <Widget>[
Expanded(
child: CircleAvatar(
radius: 75,
child: BlocBuilder<ImagePickerCubit, ImagePickerState>( //PROBLEM IS HERE
bloc: cubit,
builder: (BuildContext context,ImagePickerState state) {
if(state is ImagePickerInitialize){
return Container(
decoration: BoxDecoration(
color: Colors.grey[200],
borderRadius: BorderRadius.circular(70)),
width: 135,
height: 135,
child: Icon(
Icons.camera_alt,
color: Colors.grey[800],
),
);
}
if(state is ImagePickerSuccess){
_image = state.image;
return ClipRRect(
borderRadius: BorderRadius.circular(70),
child: Image.file(
File(_image!.path),
),
);
}
if(state is ImagePickerFailure){
return Text(state.errorMessage);
}
return const SizedBox();
},
),
),
),
Column(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
ListTile(
leading: const Icon(Icons.photo_library),
title: const Text('Pick photo from gallery'),
onTap: () {
Navigator.pop(context);
cubit.takePhotoFromGallery();
},
),
ListTile(
leading: const Icon(Icons.photo_camera),
title: const Text('Take photo from camera'),
onTap: () {
Navigator.pop(context);
cubit.takePhotoFromCamera();
},
),
],
),
],
);
}
}

在此先感谢您的帮助!

最佳答案

只需将通用参数添加到您的 Cubit 中,例如:

class ImagePickerCubit extends Cubit<ImagePickerState> {
...

关于flutter - 'ImagePickerCubit' 不符合类型参数 'BlocBase<ImagePickerState>' 的绑定(bind) 'B',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68883752/

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