title: Flutter Dart setState not working: Unhandled Exception: setState() called in constructor: ...(lifecycle state: created, no widget, not mounted)
body: <p>I am new to dart / flutter. I am changing the default app created by <strong>flutter create</strong> command. This is the code (I want to show information from EventSource):</p>
  <pre><code>import 'dart:async';
  import 'package:w3c_event_source/event_source.dart';
  import 'package:flutter/material.dart';
  import 'blocs/main_bloc.dart';
  
  void main() async {
      runApp(MyApp());
      final events = EventSource(Uri.parse(&quot;https://example.com/broadcast/&quot;));
      final subscription = events.events.listen((MessageEvent message) {
        var myhomepagestate = MyHomePageState();
        myhomepagestate.eventsource_message = '${message.data}';
        //print('${message.data}');
      });  
    
  }
  
  class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      return MaterialApp(
        title: 'EXAMPLE',
        home: MyHomePage(title: 'EXAMPLE'),
      );
    }
  }
  
  class MyHomePage extends StatefulWidget {
    MyHomePage({Key key, this.title}) : super(key: key);
    final String title;
  
    @override
    MyHomePageState createState() =&gt; MyHomePageState();
  }
  
  class MyHomePageState extends State&lt;MyHomePage&gt; {
    var _myeventsource = &quot;&quot;;
    
    void set eventsource_message(message) {
      //if (!mounted) return;
      setState(() {
        _myeventsource = message;
      });
    }
  
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: &lt;Widget&gt;[
              Text(
                'EventSource:',
              ),
              Text(
                '$_myeventsource',
              ),
            ],
          ),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: _incrementCounter,
          tooltip: 'Increment',
          child: Icon(Icons.add),
        ),
      );
    }
  }
  </code></pre>
  <p>The error is at this command:</p>
  <pre><code>setState(() {
    _myeventsource = message;
  });
  </code></pre>
  <p>The error is: <strong>Unhandled Exception: setState() called in constructor: MyHomePageState#8034c(lifecycle state: created, no widget, not mounted)</strong></p>
  <p>I have searching for one day and can't find the solution. Can anybody give a hint what I should look into? Can I just name the element, then change the element directly like HTML DOM?</p>
  <p>Thanks.</p>