简体   繁体   English

如何访问不在树中构建小部件的 dart 文件中的继承小部件?

[英]How do I access my inherited widget in a dart file that doesn't build a widget in the tree?

I'm following this example to add my very first InheritedWidget into my app.我正在按照这个示例将我的第一个InheritedWidget添加到我的应用程序中。 I've created my _state_container.dart file and now want to use it.我已经创建了我的_state_container.dart文件,现在想使用它。

To start, I'd like to use it in the widget where I display the chats in a group chat.首先,我想在群聊中显示聊天的小部件中使用它。 This widget is in the file receivedChats.dart (chat header).此小部件位于文件receivedChats.dart (聊天标题)中。 I see in the example that all I would need to do is instantiate the container in the target's widget build function, but the problem is that this widget is calling another file called message_dao.dart to handle all data from Firebase.我在示例中看到,我需要做的就是在目标的小部件构建 function 中实例化container ,但问题是这个小部件正在调用另一个名为message_dao.dart的文件来处理来自 Z0354489FF8D0324EF544A 的所有数据This file doesn't have a widget build function, so how would I use the container here?该文件没有小部件构建 function,那么我将如何在这里使用容器?

File Structure:文件结构:

main.dart
|
wrapper.dart
|
home.dart //or authenticate.dart if user is not logged in
|
chatScreen.dart
|               |
msgInput.dart   receivedChats.dart - message_dao.dart

This is what I've already tried, but it gives 2 errors:这是我已经尝试过的,但它给出了2个错误:

class MessageDao {

  final container = StateContainer.of(context); //ERROR: Undefined name 'context'. 
  final chatState = container.chatState; //ERROR: The instance member 'container' can't be accessed in an initializer. 

  //...rest of code...//

its a class, Dart classes are the blueprint of the object, or it can be called object constructors.它的一个 class、Dart 类是 object 的蓝图,也可以称为 ZA8CFDE6331B49EB2AC96B8 构造函数。 its just not initialize yet.它只是还没有初始化。 just like that StateContainer it self;就像那个StateContainer一样;

class MessageDao {
 //here :
 final StateContainerState container;
 
 MessageDao(this.container);

 chatstate(){
 return container.chatState;
 }
}

you need to initialize it somewhere in your code when there is an actual build context that can be passed to:当存在可以传递给的实际构建上下文时,您需要在代码中的某处对其进行初始化:

final messageDaoState = MessageDao(StateContainer.of(context));

final chatstate = messageDaoState.chatstate();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在小部件构建方法运行之前,如何在初始值未知的 bool 上使用 setState? Flutter Firebase - How do I use setState on a bool of which the initial value isn't know until after the widget build method runs? Flutter Firebase 为什么 FutureBuilder 不返回 Widget? - Why doesn't FutureBuilder return the Widget? Flutter Firestore 在小部件树中找不到提供程序? - Flutter Firestore can't find provider in the widget tree? 如何在谷歌地图 flutter 的初始化程序中访问小部件 - How to access widget in an initializer in google maps flutter Flutter stream builder 不更新 Widget - Flutter stream builder doesn't update Widget 如何在 Flutter dart 的 ListView 中添加 floatingactionbutton - How do I add floatingactionbutton in my ListView in Flutter dart Flutter:当小部件没有父/子关系时,如何从小部件 B 调用小部件 A 中的 function? - Flutter: How do I call a function in widget A from widget B when the widgets do not have a parent/child relationship? 仅当集合在 flutter 中有文档时,如何使小部件可见? - How do i make a widget visible only if the collection has docs in flutter? Flutter JIT编译和动态widget树 - Flutter JIT compilation and dynamic widget tree 如果我移动到另一个小部件,则无法使用“_auth.signOut()” - Can't use the "_auth.signOut()" if I move to another widget
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM