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

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

问题描述

我希望在Flutter应用程序的根级别使用InheritedWidget,以确保所有子窗口小部件都可以使用经过身份验证的用户的详细信息.本质上,将脚手架做成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 '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作为父级

=>崩溃

诀窍是使用不同的 context . 那里有很多解决方案.您可以在另一个小部件中实例化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(),
  ),
);


出于相同的原因,另一个问题是,如果在路由的内部插入继承的窗口小部件,则该路由的外部将不可用.


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 上方 MaterialApp.

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

上方材料:

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

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

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