我正在重新撰寫 Reso Coder 的 TDD 清潔架構課程,并進行了一些小改動。最終目標是擁有與最新版本的 Flutter 完全相同的內容和更新的庫,并且還可以選擇 Riverpod 狀態管理。但是在使用時flutter_bloc我似乎無法使其作業。
BlocProvider buildBody({required BuildContext context}){
return BlocProvider(
create: (context) => sl<NumberTriviaBloc>(),
child: Center(
child: Padding(
padding: const EdgeInsets.all(10.0),
child: Column(
children: [
SizedBox(height: 20.0,),
Container(
margin: EdgeInsets.all(5),
height: MediaQuery.of(context).size.height/2.5,
child: Placeholder()
),
SizedBox(height: 20.0,),
Column(
children: [
Placeholder(fallbackHeight: 50),
SizedBox(height: 25),
Row(
children: [
Expanded(child: Placeholder(fallbackHeight: 30)),
SizedBox(width: 10),
Expanded(child: Placeholder(fallbackHeight: 30),),
],
)
],
)
],
),
),
),
) ;
}
上面的代碼似乎作業正常,沒有任何錯誤。
但是一旦我介紹了 BlocBuilder (下面給出的片段),我得到
Error: Could not find the correct Provider<NumberTriviaBloc> above this BlocBuilder<NumberTriviaBloc, NumberTriviaState> Widget
BlocProvider buildBody({required BuildContext context}){
return BlocProvider(
create: (context) => sl<NumberTriviaBloc>(),
child: Center(
child: Padding(
padding: const EdgeInsets.all(10.0),
child: Column(
children: [
SizedBox(height: 20.0,),
//start of the problem
BlocBuilder<NumberTriviaBloc, NumberTriviaState>(
builder: (context, state) {
if (state is Empty) {
return Text("1");
}else{
return Text("2");
}
}
),
//end of the problem
Container(
margin: EdgeInsets.all(5),
height: MediaQuery.of(context).size.height/2.5,
child: Placeholder()
),
SizedBox(height: 20.0,),
Column(
children: [
Placeholder(fallbackHeight: 50),
SizedBox(height: 25),
Row(
children: [
Expanded(child: Placeholder(fallbackHeight: 30)),
SizedBox(width: 10),
Expanded(child: Placeholder(fallbackHeight: 30),),
],
)
],
)
],
),
),
),
) ;
}
我已經閱讀了我可以從互聯網上獲得的所有內容,但似乎仍然無法弄清楚。任何幫助將非常感激。
完整代碼可在此處獲得,依賴注入邏輯可在此處獲得
uj5u.com熱心網友回復:
您還必須將集團傳遞給BlocBuilder。
bloc: sl<NumberTriviaBloc>(),
只需將此添加到您的BlocBuilder,您就可以開始了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/408832.html
標籤:
