close
0

how to access an object created in one stateful widget in another stateful widget in flutter

avatar
asked 13-09-2021

1 Answers


0

Add model to SecondScreen constructor:

class SecondScreen extends StatefulWidget {
  final MyModel myModel;

  SecondScreen(MyModel myModel, {Key key}):
      super(key: key);
...
}

Pass model when you construct SecondScreen in main.dart

Navigator.push(context, new MaterialPageRoute(builder: (_) => 
  new SecondScreen(model)));

Now you can access model in _SecondScreenState via widget.myModel

avatar
answered 14-09-2021