带有脚手架的 InheritedWidget 作为孩子似乎不起作用 [英] InheritedWidget with Scaffold as child doesn't seem to be working

查看:18
本文介绍了带有脚手架的 InheritedWidget 作为孩子似乎不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在 Flutter 应用程序的根级别使用 InheritedWidget,以确保所有子小部件都可以使用经过身份验证的用户的详细信息.基本上让 Scaffold 像这样成为 IW 的孩子:

I was hoping to use InheritedWidget at the root level of my Flutter application to ensure that an authenticated user's details are available to all child widgets. Essentially making the Scaffold the child of the IW like this:

@override
Widget build(BuildContext context) {
return new AuthenticatedWidget(
    user: _user,
    child: new Scaffold(
      appBar: new AppBar(
        title: 'My App',
      ),
      body: new MyHome(),
      drawer: new MyDrawer(),
    ));
}

这在应用程序启动时按预期工作,因此从表面上看,似乎我已经在我的 AuthenticatedWidget 中正确实现了 InheritedWidget 模式,但是当我从其他地方返回主页 (MyHome) 时,例如这个:

This works as expected on app start so on the surface it seems that I have implemented the InheritedWidget pattern correctly in my AuthenticatedWidget, but when I return back to the home page (MyHome) from elsewhere like this:

Navigator.popAndPushNamed(context, '/home');

这个调用 MyHome 的构建方法(以前工作过)然后导致 authWidget 为空:

This call-in the build method of MyHome (which worked previously) then results in authWidget being null:

final authWidget = AuthenticatedWidget.of(context);

完全有可能我错过了如何正确实施 IW 的一些细微差别,但同样,它最初确实有效,我也看到其他人提出了同样的问题(即 此处 在继承的小部件"标题下).

Entirely possible I'm missing some nuances of how to properly implement an IW but again, it does work initially and I also see others raising the same question (i.e. here under the 'Inherited Widgets' heading).

因此,是否不能将 Scaffold 或 MaterialApp 用作 InheritedWidget 的子项?或者这可能是一个要提出的错误?提前致谢!

Is it therefore not possible to use a Scaffold or a MaterialApp as the child of an InheritedWidget? Or is this maybe a bug to be raised? Thanks in advance!

推荐答案

MyInherited.of(context) 基本上会查看当前上下文的父级,看看是否有一个 MyInherited 实例化.

MyInherited.of(context) will basically look into the parent of the current context to see if there's a MyInherited instantiated.

问题是:您继承的小部件在当前上下文实例化.

The problem is : Your inherited widget is instantiated within the current context.

=> 没有 MyInherited 作为父级

=> No MyInherited as parent

=> 崩溃

诀窍是使用不同的上下文.那里有很多解决方案.您可以在另一个小部件中实例化 MyInherited,这样构建方法的 context 将有一个 MyInherited 作为父级.

The trick is to use a different context. There are many solutions there. You could instantiate MyInherited in another widget, so that the context of your build method will have a MyInherited as parent.

或者,您可以潜在地使用 Builder 来引入一个假小部件,该小部件将传递给您它的上下文.

Or you could potentially use a Builder to introduce a fake widget that will pass you it's context.

构建器示例:

return new MyInheritedWidget(
  child: new Builder(
    builder: (context) => new Scaffold(),
  ),
);

<小时>

另一个问题,出于同样的原因,如果你在路由内部插入一个继承的Widget,它在该路由的外部将不可用.


Another problem, for the same reasons, is that if you insert an inheritedWidget inside a route, it will not be available outside of this route.

这里的解决方案很简单!把你的 MyInheritedWidget above MaterialApp.

The solution is simple here ! Put your MyInheritedWidget above MaterialApp.

以上材料:

new MyInherited(
  child: new MaterialApp(
    // ...
  ),
)

这篇关于带有脚手架的 InheritedWidget 作为孩子似乎不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆