我无法弄清楚这段代码有什么问题。此页面上有一个 bloc 提供程序:
Widget build(BuildContext context) {
return MultiBlocProvider(
providers: [
BlocProvider(
create: (BuildContext context) {
return DatabaseBloc();
},
),
BlocProvider(
create: (BuildContext context) {
return QuestionholderBloc();
},
)
],`child:RaisedButton(
splashColor: Colors.green[600],
focusElevation: 50.0,
hoverElevation: 50.0,
color: Colors.green[600],
shape: RoundedRectangleBorder(
side: BorderSide(color: Colors.yellow[700]),
borderRadius: BorderRadius.circular(50.0)),
child: Text(
"Start",
style: TextStyle(fontWeight: FontWeight.bold),
),
textColor: Colors.white70,
onPressed: () {
ExtendedNavigator.of(context)
.pushNamed(Routes.questionHandler);
}),)
对于可能的语法错误,我们深表歉意。在 QuestionHolderPage 我有:
void didChangeDependencies() {
_statusbloc = BlocProvider.of<QuestionholderBloc>(context);
_dbbloc = BlocProvider.of<DatabaseBloc>(context);
_statusbloc.add(CheckStatus());
super.didChangeDependencies();
}
Widget build(BuildContext context) {
return Scaffold(
body: MultiBlocListener(
listeners: [
BlocListener<QuestionholderBloc, QuestionholderState>(
listener: (context, state) {
if (state is NoQuestions) {
_dbbloc.add(GetQuestions());
} else if (state is HasQuestions) {
_statusbloc.add(GetQuestion());
}
},
),
BlocListener<DatabaseBloc, DatabaseState>(
listener: (context, state) {
if (state is Loaded) {
_statusbloc.add(SetQuestions(questionsToSet: state.questions));
}
},
),
],
child: BlocBuilder<QuestionholderBloc, QuestionholderState>(
builder: (context, state) {
if (state is QuestionLoaded) {
return QuestionWidget(question: state.question);
} else {
return CircularProgressIndicator();
}
},
),
),
);
}
}
错误:
错误:从传递给 I/flutter (4318) 的上下文开始找不到祖先:BlocProvider.of()。