- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我正在调用 InheritedWidget
的 of
函数,但是当您在上面看到我的小部件位于顶部时,我的函数返回 null那个树。调用来自被推送到导航器堆栈的页面,而不是该页面。有谁知道为什么?我的 InheritedWidget
代码如下。
class LiveKHProvider extends InheritedWidget {
final LiveKHBloc liveKHBloc = LiveKHBloc();
@override
bool updateShouldNotify(InheritedWidget oldWidget) => oldWidget != this;
static LiveKHBloc of(BuildContext context) {
var inheritFromWidgetOfExactType =
context.inheritFromWidgetOfExactType(LiveKHProvider); // to clearly see what’s returning null.
//This is where it returns null,
//so the below line is executed on a null object.
return (inheritFromWidgetOfExactType as LiveKHProvider).liveKHBloc;
}
LiveKHProvider({Key key, Widget child}) : super(key: key, child: child);
}
最佳答案
至少在我的测试中,InheritedWidget 可以被继承,即使它是 MaterialApp/WidgetsApp/CupertinoApp 或路由的父级。
对于遇到此问题的其他人,更可能的解释是您导入了具有不同大小写的 InheritedWidget。即在您生成它的地方,您可能已将其导入为 import 'MyInheritedWidget.dart'
,而在您尝试检索它的地方,您将其导入为 import 'myinheritedwidget.dart'
.
这不会导致任一文件出错(至少在 Windows 中),但不会匹配并返回 null。
关于dart - 尝试使用 InheritedWidget 但获取 Null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51717476/
简介:我认为这个问题原则上可能是共享的,但我觉得这个具体的应用程序不是。我谦虚地提交它供考虑,所以请温柔。我的代码在这里陷入了一个明显的陷阱,而且我根本不知道如何修复它。想了几个小时后,我什至不知道该
flutter documentation for InheritedWidget说 Base class for widgets that efficiently propagate informa
我想知道是否有人知道如何知道 InheritedWidget 何时被释放? 这个问题的原因是我正在做一些实验,并且我正在使用 InheritedWidget 作为 BLoC 的提供者。此 BLoC 在
我是不是错了,或者如果我们只是想将一个值传递到 Widget 树下,Provider只是一个带有 dispose 方法的 InheritedWidget 吗? 最佳答案 是的。 Provider 确实
我正在使用继承的 Widget 来访问具有一些长时间运行任务(例如搜索)的 Bloc。我想在第 1 页触发搜索并在完成后继续到下一页。因此,我正在监听流并等待结果发生,然后导航到结果页面。现在,由于使
我有以下设置: MaterialApp(home:SplashScreen) SplashScreen( ///do some auth checks then Navigator.of(contex
我正在使用 Rx_command、Rx_dart 使用响应式组件流/可观察对象进行开发 问题: 在我的 Flutter 应用中,我继承了可以在任何地方调用的小部件: FooProvider.of(co
我正在尝试将数据从一个小部件传递到树下的另一个小部件。这是我从一个屏幕导航到另一个屏幕的代码: final page = LanguagePreferenceProvider(child: UserN
我正在尝试查看是否可以添加一些在我的应用程序周围传递的通用导航行为,并且我发现 InheritedWidget 是避免在小部件树周围传递特定回调的绝佳选择,但是我越来越注意到我只能拥有特定类类型的 I
我是 Flutter 的新手,对 InheritedWidget 如何与路由一起工作感到困惑。我正在使用带有 sqflite 库的 SQLite 数据库。基本上,我想要实现的是,当我的应用程序启动时,
我看到我可以像这样在 build() 方法中访问 InheritedWidgets:final inheritWidget = ChronoApp.of(context); 但是如果我想访问它在其他地
我正在调用 InheritedWidget 的 of 函数,但是当您在上面看到我的小部件位于顶部时,我的函数返回 null那个树。调用来自被推送到导航器堆栈的页面,而不是该页面。有谁知道为什么?我的
可重现代码: void main() => runApp(MaterialApp(home: CountInheritedWidget(child: HomePage()))); class Coun
我在flutter中的代码实现InheritedWidget/InheritedModel是推荐的。但是我有一个小问题。 假设我有一个名为 MyInheritedWidget 的继承小部件其中包含一些
我希望在我的 Flutter 应用程序的根级别使用 InheritedWidget,以确保所有子小部件都可以使用经过身份验证的用户的详细信息。基本上让 Scaffold 成为 IW 的子节点,如下所示
我希望在我的 Flutter 应用程序的根级别使用 InheritedWidget,以确保所有子小部件都可以使用经过身份验证的用户的详细信息。基本上让 Scaffold 成为 IW 的子节点,如下所示
我的应用程序根目录中有一个 InheritedWidget,基本上位于我的第一个 Scaffold 的第一个构建函数的顶部。我可以使用 .of(context) 函数从第一页的所有子小部件中引用小部件
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 4 年前。 Improve
在将 InheritedWidget 添加到应用程序后,当我使用 Pop() 从详细信息屏幕返回列表时,我发现 ListView 失去了滚动位置(所选项目索引)。 我知道小部件可以随时重建,但也许有人
在导航到新小部件后尝试访问 InheritedWidget 时遇到问题。 我有这样的顶级小部件 class App extends StatelessWidget{ build(context){
我是一名优秀的程序员,十分优秀!