'ImagePickerCubit' 不符合类型参数 'B' 的绑定 'BlocBase<ImagePickerState>'
'ImagePickerCubit' doesn't conform to the bound 'BlocBase<ImagePickerState>' of the type parameter 'B'
我在尝试在我的应用程序中创建 BlocBuilder 时遇到错误:
“'ImagePickerCubit' 不符合类型参数 'B' 的绑定 'BlocBase'。尝试使用属于 '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()}');
}
}
}
ImagePickerState:
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> {
...
我在尝试在我的应用程序中创建 BlocBuilder 时遇到错误: “'ImagePickerCubit' 不符合类型参数 'B' 的绑定 'BlocBase'。尝试使用属于 '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()}');
}
}
}
ImagePickerState:
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> {
...