扑 returns "No ancestor could be found starting from the context that was passed to I/flutter"
Flutter returns "No ancestor could be found starting from the context that was passed to I/flutter"
我不知道这段代码有什么问题。此页面上有一个 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();
}
},
),
),
);
}
}
错误:
Error: No ancestor could be found starting from the context that was passed to
I/flutter ( 4318): BlocProvider.of().
从您的代码中可以看出,您正在从初始化 MultiBlocProvider 的位置导航,因此您会收到此错误。
如果您在一个屏幕中初始化并在导航到其他屏幕后访问它,您将无法访问提供程序。
第一种解决方法
如果你想这样做,那么可以在 MaterialApp 小部件上面初始化你的 MultiBlocProvider,这样你就可以在你的应用程序的每个屏幕上访问。
第二种方式:
您可以在导航时传递要在其他屏幕中访问的所有值,并在该屏幕中初始化新的 MultiBlocProvider。
我不知道这段代码有什么问题。此页面上有一个 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();
}
},
),
),
);
}
}
错误:
Error: No ancestor could be found starting from the context that was passed to I/flutter ( 4318): BlocProvider.of().
从您的代码中可以看出,您正在从初始化 MultiBlocProvider 的位置导航,因此您会收到此错误。
如果您在一个屏幕中初始化并在导航到其他屏幕后访问它,您将无法访问提供程序。
第一种解决方法
如果你想这样做,那么可以在 MaterialApp 小部件上面初始化你的 MultiBlocProvider,这样你就可以在你的应用程序的每个屏幕上访问。
第二种方式:
您可以在导航时传递要在其他屏幕中访问的所有值,并在该屏幕中初始化新的 MultiBlocProvider。