0

I'm following this example to add my very first InheritedWidget into my app. I've created my _state_container.dart file and now want to use it.

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). 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. This file doesn't have a widget build function, so how would I use the container here?

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:

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...//
whatwhatwhat
  • 1,991
  • 4
  • 31
  • 50

1 Answers1

0

its a class, Dart classes are the blueprint of the object, or it can be called object constructors. its just not initialize yet. just like that StateContainer it self;

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();
Sayyid J
  • 1,215
  • 1
  • 4
  • 18
  • The 2nd to last line (`final messageDaoState = ...`) gives this error: `The argument type 'StateContainerState' can't be assigned to the parameter type 'StateContainer'.` – whatwhatwhat Aug 16 '22 at 19:28
  • if `StateContainer.of(context)` return type is `StateContainerState`, then you should declare as that, change final `StateContainer container` to ` final StateContainerState container;` – Sayyid J Aug 17 '22 at 03:47
  • i idited my answer for you – Sayyid J Aug 17 '22 at 03:47