InheritedWidget-在navigator.push之后在null上调用getter [英] InheritedWidget - The getter was called on null after navigator.push
问题描述
导航到新的小部件后尝试访问InheritedWidget时遇到麻烦。
I'm having trouble trying to access an InheritedWidget after navigating to a new widget.
我有这样的顶级小部件
class App extends StatelessWidget{
build(context){
return MaterialApp(
title: 'Iniciar Sesion',
home: LoginBlocProvider(child: WelcomeScreen()),
);
}
}
然后,WelcomeScreen中有一个导航到LoginScreen的按钮
Then WelcomeScreen has a button to navigate to LoginScreen
class WelcomeScreen extends StatelessWidget {
@override Widget build(BuildContext context){
return Scaffold(
body: Center(child: MyButton)
);
}
}
class MyButton extends StatelessWidget {
@override
Widget build(BuildContext context) {
return RaisedButton(
shape: StadiumBorder(),
child: Text('Ingresar', style: TextStyle(color: Colors.black)),
elevation: 5.0,
onPressed: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (BuildContext context) =>LoginScreen()
));
}
);
}
}
最后在LoginScreen中,我想访问InheritedWidget
Finally in LoginScreen I want to access the InheritedWidget
class LoginScreen extends StatefulWidget {
@override
_LoginScreenState createState() => _LoginScreenState();
}
class _LoginScreenState extends State<LoginScreen> {
LoginBloc bloc;
@override void didChangeDependencies() {
bloc = LoginBlocProvider.of(context);
super.didChangeDependencies();
}
@override Widget build(BuildContext context){
return Scaffold(
body:
Stack(
fit: StackFit.expand,
children: <Widget>[
Positioned(
top: 0.0,
child: Image.asset('assets/images/img.jpg',
fit: BoxFit.none,
),
),
_buildLogin(),
],
),
);
}
}
编辑后: LoginBlocProvider
Edited: Here it's the LoginBlocProvider
class LoginBlocProvider extends InheritedWidget {
final bloc;
LoginBlocProvider({Key key, Widget child})
: bloc = LoginBloc(),
super(key:key, child:child);
@override
bool updateShouldNotify(InheritedWidget oldWidget) => true;
static LoginBloc of(BuildContext context) {
return (context.inheritFromWidgetOfExactType(LoginBlocProvider) as LoginBlocProvider).bloc;
}
}
但是,当我运行.of方法时InheritedWidget我收到此错误
But, when I run the .of method of the InheritedWidget I get this error
I/flutter (27725): The following NoSuchMethodError was thrown building Builder:
I/flutter (27725): The getter 'bloc' was called on null.
I/flutter (27725): Receiver: null
I/flutter (27725): Tried calling: bloc
我的印象是,这都与Navigator.push构建器方法中的上下文有关。
因为如果我在没有Navigator.push的情况下使用LoginScreen小部件,则可以很好地使用InheritedWidget
I have the impression that it all has to do with the context in the Navigator.push builder method. Because if I use the LoginScreen widget without the Navigator.push, I can use the InheritedWidget perfectly fine
由于将上下文传递给 LoginBlocProvider.of()
方法未找到实例。
The error is happening because the context passed to the LoginBlocProvider.of()
method is not finding the instance.
对此有任何想法吗?
推荐答案
在您提供的代码中, LoginScreen
不是<$ c $的后代c> LoginBlocProvider 这就是为什么找不到祖先小部件的原因。您的代码将 WelcomeScreen
路径包装在 LoginBlocProvider
中,而不是整个导航器中。解决方案是将 MaterialApp
包装在 LoginBlocProvider
中,然后您将可以在应用程序的任何位置访问它。
In the code you've provided, LoginScreen
is not a descendant of LoginBlocProvider
which is why it can't find the ancestor widget. Your code wraps the WelcomeScreen
route in LoginBlocProvider
, but not the whole navigator. The solution is to wrap your MaterialApp
in LoginBlocProvider
and then you will have access to it everywhere in your app.
class App extends StatelessWidget {
@override
Widget build(context) {
return LoginBlocProvider(
child: MaterialApp(
title: 'Iniciar Sesion',
home: WelcomeScreen(),
),
);
}
}
这篇关于InheritedWidget-在navigator.push之后在null上调用getter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!