title: Can't pass updated state of variable dynamically from screen/page 1 to screen/page 2
body: <p>I created an instantiated an object (speech) in screen 1 and then called the initialization method for that object, where I set the error handling (var <code>lastError</code> gets the error output).  Then I passed to screen 2 the object as an argument using <code>Navigator.pushNamed</code> where I call some other speech methods.</p>
  <p>The problem I'm facing is that when an error arise while being in screen 2, I can't get the error output since the initialize and error catching was defined in screen 1.</p>
  <p>What I have tried so far:</p>
  <ol>
  <li>Passing the <code>lastError</code> via GlobalKey, but it only passes the state at the time the widget of screen 2 is build, but not dinamically as it does in screen 1.</li>
  <li>Adding the errorHandling method in screen 2 but always the object calls the errorHandling method in screen 1.</li>
  <li>Adding setState for the key.currentState.lastError, but anyway it is not  dynamically updated.</li>
  </ol>
  <p>I can’t pass the initialize calling to page 2 since I need to confirm the object is working in screen 1 prior to pass to screen 2 to record voice.</p>
  <p>Any advice how to get the error updated dynamically in screen 2?</p>
  <pre><code>class PassedSpeechComponent {
    stt.SpeechToText speech;
    String lastError;
    PassedSpeechComponent(this.speech, this.lastError, this.lastStatus);
  }
  
  final key = new GlobalKey&lt;_MainPgState&gt;();
  
  void main() {
    runApp(MaterialApp(
      title: 'Routes',
      initialRoute: '/',
      routes: {
        '/': (context) =&gt; MainPg(key: key),
        '/speech': (context) =&gt; SpeechCapturePg(),
      },
    ));
  }
  
  class MainPg extends StatefulWidget {
    MainPg({ Key key }) : super(key: key);
    @override
    _MainPgState createState() =&gt; _MainPgState();
  }
  
  // ***** SCREEN 1 ******
  class _MainPgState extends State&lt;MainPg&gt; {
    var lastError = &quot;&quot;;
    var lastStatus = &quot;&quot;;
    bool _available = false;
    stt.SpeechToText speech = stt.SpeechToText();
  
    // speech error listener of 1st screen
    void errorListener(SpeechRecognitionError error) { 
       setState(() {
        lastError = &quot;${error.errorMsg} - ${error.permanent}&quot;;
        print('@@@ Called error listener of 1st screen @@@');
      });
      speech.stop();
    }
  
    Future&lt;void&gt; initSpeechState() async {
      bool available = await speech.initialize(
         onError: errorListener);
        setState(() {
        _available = available;
      });
    }
  
    @override
    Widget build(BuildContext context) {
      if (!_available) {
        initSpeechState();
      } 
      return Scaffold(
        appBar: AppBar(
          title: Text(&quot;--&quot;),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: &lt;Widget&gt;[
              Text('Initializing:$_available',
                  style: TextStyle(
                    color: _available ? Colors.green : Colors.black,
                    fontSize: 15,
                  )),
              Text('isListening:${speech.isListening}',
                  style: TextStyle(
                    color: speech.isListening ? Colors.green : Colors.black,
                    fontSize: 15,
                  )),
                  Text('Error:' + lastError, // LastError alert on first screen working as intended.
                      style: TextStyle(
                        color:
                          lastError != &quot;&quot; ? Colors.red : Colors.black,
                          fontSize: 15,
                  )),
              Align(        
                child: Stack(
                    alignment: AlignmentDirectional.bottomCenter,
                    children: &lt;Widget&gt;[
                      SizedBox(
                        width: 110.0,
                        height: 110.0,
                        child: Visibility(
                          visible: !speech.isListening,
                          child: FloatingActionButton(
                            onPressed: () {
                              if (_available) {
                                Navigator.pushNamed(
                                  context,
                                  '/speech',
                                  arguments: {speech, errorListener, statusListener},
                                );
                              } else {
                                initSpeechState();
                              }
                            },
                            tooltip: 'A single tap is enough',
                            child: Icon(Icons.mic),
                          ),
                        ),
                      ),
                    ]),
              ),
            ],
          ),
        ),
        );
    }
  }
  
  // ******** SECOND SCREEN ********
  class SpeechCapturePg extends StatefulWidget {
    final String title;
  
  const SpeechCapturePg({Key key, this.title}) : super(key: key);
    @override
    _SpeechCapturePgState createState() =&gt; _SpeechCapturePgState();
  }
  
  class _SpeechCapturePgState extends State&lt;SpeechCapturePg&gt;
      with TickerProviderStateMixin {
    String lastWords = &quot;&quot;;
    String lastError = &quot;&quot;;
    
    @override
    void initState() {
      super.initState();
     
      WidgetsBinding.instance.addPostFrameCallback((_) {
        startListening(ModalRoute.of(context).settings.arguments);
      });
  
        setState(()
        {
          // Here I try to update dinamically lastError anytime 
          // last error of first screen is updated, but is not working... =(
          lastError = key.currentState.lastError;
          print(&quot;Last Error&quot; + key.currentState.lastError);
        });
    }
  
    @override
    dispose() {
      super.dispose();
    }
  
    // This method is never called in this screen/page, just the one created in screen 1 
    void errorListener(SpeechRecognitionError error) {
      setState(() {
        print('Called in 2nd screen:'$lastError);
      });
    }
  
    
    void startListening(speech) {
      lastWords = &quot;&quot;;
      lastError = &quot;&quot;;    
      // 
      speech.first.listen(
        onResult: resultListener,
        cancelOnError: true,
      );
    }
  
    void resultListener(SpeechRecognitionResult result) {
      setState(() {
        lastWords = &quot;${result.recognizedWords}&quot;;
      });
    }
  
    @override
    Widget build(BuildContext context) {
      return Scaffold(
          backgroundColor: Colors.redAccent,
          body: Center(
              child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: &lt;Widget&gt;[
                    Expanded(
                      flex: 30,
                      child: Padding(
                        padding: const EdgeInsets.all(16.0),
                        child:
                          Text('$lastWords')
                    )),
                    //***** HERE I NEED TO SHOW THE ERROR REPORTED BY OBJECT *****/                        
                    Text('Error - $lastError'),
                    // **** 2ND TRY TO SHOW ERROR REPORTED IN FIRST SCREEN
                    Text('Error' + key.currentState.lastError),                  
              ])));
    }
  }
  
  </code></pre>