引发了另一个异常:错误:在此 BlocSelector<HCubit, HState, HState> Widget 上方找不到正确的 Provider<HState>
Posted
技术标签:
【中文标题】引发了另一个异常:错误:在此 BlocSelector<HCubit, HState, HState> Widget 上方找不到正确的 Provider<HState>【英文标题】:Another exception was thrown: Error: Could not find the correct Provider<HState> above this BlocSelector<HCubit, HState, HState> Widget 【发布时间】:2021-11-16 22:09:08 【问题描述】:我在freezd 中使用cubit。我在底部表格中有一个输入文本表单字段,一切都很完美,但是一旦我单击输入表单字段并输入任何字母,就会发生此异常。
抛出了另一个异常:错误:在此 BlocSelector
文本表单域代码为:
class TextInput extends StatelessWidget
@override
Widget build(BuildContext context)
TextEditingController _textEditingController = TextEditingController();
return BlocSelector<HCubit, HState, String?>(
selector: (state)
return state.maybeMap(
orElse: () ,
inputTextValue: (val) =>
val.inputTextValue.input.fold((l) => l.msg, (r) => null));
,
builder: (context, state)
return TextFormField(
textAlign: TextAlign.start,
onChanged: (String value) => context
.read<HState>()
.maybeMap(inputTextValue: (value) => value, orElse: () ),
validator: (String? validator) => context
.read<HState>()
.maybeWhen(
orElse: () ,
inputTextValue: (val) =>
val.input.fold((l) => l.msg, (r) => null)),
);
,
),
);
素材应用代码:
class AppWidget extends StatelessWidget
@override
Widget build(BuildContext context)
return Sizer(
builder: (context, orientation, deviceType)
return MaterialApp(
BlocProvider<HCubit>(
lazy: false,
create: (context) => HCubit(),
child: const
HomePage(),
),
);
,
);
区号为:
class HCubit extends Cubit<HState>
HCubit() : super(const HState.initial());
void addNewNoteButton(required bool isClicked) =>
emit(HState.isClicked(isClicked: isClicked));
void textInputNote(required String textNote) =>
emit(HState.inputTextValue(inputTextValue: Note(textInput: textNote)));
状态码是:
abstract class HState with _$HState
const factory HState.initial() = _Initial;
const factory HState.inputTextValue(required Note inputTextValue)=InputTextValue;
const factory HState.isClicked(required bool isClicked)=IsClicked;
【问题讨论】:
【参考方案1】:在您的材料应用程序代码中有语法错误应该是这样的:
class AppWidget extends StatelessWidget
@override
Widget build(BuildContext context)
return Sizer(
builder: (context, orientation, deviceType)
return MaterialApp(
home: BlocProvider<HCubit>(
lazy: false,
create: (context) => HCubit(),
child: const
HomePage(),
),
);
,
);
【讨论】:
以上是关于引发了另一个异常:错误:在此 BlocSelector<HCubit, HState, HState> Widget 上方找不到正确的 Provider<HState>的主要内容,如果未能解决你的问题,请参考以下文章
引发了另一个异常: RenderBox 未布置:CustomRenderShrinkWrappingViewport#f6727 relayoutBoundary=up17 NEEDS-PAINT N
处理您的请求时发生异常。此外,在执行自定义错误页面时发生了另一个异常