title: how to access an object created in one stateful widget in another stateful widget in flutter
body: <p>I am stuck in my project where I have two stateful widgets created in flutter as two different dart files. Now, I have to access the instance of an object created in first widget in the second widget, but I am not so sure how I can do this in flutter when creating widgets.</p>
  
  <p>One possible solution I thought of is to declare the two widgets in just one dart file instead of two dart files for two layouts, but I am curious if we can do it by declaring in two separate dart files.</p>
  
  <p>I have posted the files just to re-create the issue.</p>
  
  <p>main.dart</p>
  
  <pre><code> import 'package:flutter/material.dart';
   import 'package:untitled2/models.dart';
   import 'package:untitled2/secondwidget.dart';
  
  void main() {
   runApp(new MaterialApp(
       home: new MyApp(),
   ),);
  }
  
  class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() =&gt; new _MyAppState();
    }
  
        class _MyAppState extends State&lt;MyApp&gt; {
   final TextEditingController _nameController = new TextEditingController();
   final TextEditingController _emailIdController = new 
   TextEditingController();
   final TextEditingController _passwordController = new 
   TextEditingController();
   final TextEditingController _confirmPasswordController = new 
   TextEditingController();
  
   MyModel myModel = new MyModel();
  
   @override
   Widget build(BuildContext context) {
    return new MaterialApp(
    home: new Scaffold(
      body: new ListView(
  
        children: &lt;Widget&gt;[
          new Container(
            child: new TextFormField(
              decoration: new InputDecoration(
                labelText: 'Enter your Name'
              ),
              controller: _nameController,
              onSaved: (String value){myModel.name = value;} ,
            ),
          ),
          new Container(
            child: new TextFormField(
              decoration: new InputDecoration(
                  labelText: 'EmailId'
              ),
                controller: _emailIdController,
                onSaved: (String value){myModel.emailId = value;}
            ),
          ),
          new Container(
            child: new TextFormField(
              decoration: new InputDecoration(
                  labelText: 'Password'
              ),
                controller: _passwordController,
                onSaved: (String value){myModel.password = value;}
            ),
          ),
          new Container(
            child: new TextFormField(
              decoration: new InputDecoration(
                  labelText: 'Confirm Password'
              ),
              controller: _confirmPasswordController,
  
            ),
          ),
          new Container(
            child: new FlatButton(
              onPressed: ((){
  
                Navigator.push(context, new MaterialPageRoute(builder: (_) =&gt; 
           new SecondScreen(),),);
  
              }),
              child: new Text('Save'),),
          ),
  
  
  
        ],
  
  
        ),
       ),
      );
     }
    }
  </code></pre>
  
  <p>models.dart</p>
  
  <pre><code>class MyModel {
  
  String name;
  String emailId;
  String password;
  
    }
  </code></pre>
  
  <p>secondwidget.dart</p>
  
  <pre><code> import 'package:flutter/material.dart';
  
  
   class SecondScreen extends StatefulWidget {
   @override
   _SecondScreenState createState() =&gt; new _SecondScreenState();
  }
  
  class _SecondScreenState extends State&lt;SecondScreen&gt; {
  @override
  Widget build(BuildContext context) {
  return new MaterialApp(
    home: new Scaffold(
      body: new ListView(
  
        children: &lt;Widget&gt;[
          new Container(
            child: new TextFormField(
              decoration: new InputDecoration(
                  labelText: 'Enter address'
              ),
            ),
          ),
          new Container(
            child: new TextFormField(
              decoration: new InputDecoration(
                  labelText: 'Address Line 2'
              ),
            ),
          ),
          new Container(
            child: new TextFormField(
              decoration: new InputDecoration(
                  labelText: 'Address Line 3'
              ),
            ),
          ),
          new Container(
            child: new TextFormField(
              decoration: new InputDecoration(
                  labelText: 'POST CODE'
              ),
            ),
          ),
  
          new Container(
            child: new TextFormField(
              decoration: new InputDecoration(
                  labelText: 'Mobile Number'
              ),
            ),
          ),
          new Container(
            child: new FlatButton(
                onPressed: ((){
  
  
                  //I want to push the data captured from main.dart and from 
                              //secondwidget.dart into database
       // I want to use the instance of MyModel from main.dart here to save 
        // the data captured from the first screen and this one into database
  
                }),
                child: new Text('Save'),),
          ),
  
  
        ],
  
  
      ),
    ),
  );
  }
  }
  </code></pre>