title: Singleton in Flutter gives runtime error "Unhandled Exception: Reading static variable '_instance@545324594' during its initialization"
body: <p>I tried to implement the MVP-Pattern in a Flutter App (Flutter version 1.12.13+hotfix8).
  My controller (presenter) is a singleton and looks like this:</p>
  
  <pre class="lang-dart prettyprint-override"><code>import 'package:flutter/material.dart';
  
  // local file imports
  import 'package:prototype/gui/screens/welcome/welcome.dart';
  
  // this is a singleton
  class Controller  {
    static final Controller _instance = Controller._internal();
    Widget _currentWidget;
  
    Controller._internal() {
      this._currentWidget = ScreenWelcome();
    }
  
    factory Controller() =&gt; _instance;
  
    Widget get currentWidget =&gt; this._currentWidget;
  
    set currentWidget(Widget widget){
      _currentWidget = widget;
    }
  }
  </code></pre>
  
  <p>My home screen looks like:</p>
  
  <pre class="lang-dart prettyprint-override"><code>import 'package:flutter/material.dart';
  import 'package:flutter/services.dart';
  
  // local file imports
  import 'package:prototype/controller/conroller.dart';
  import 'package:prototype/gui/screens/register/register.dart';
  import 'package:prototype/gui/screens/register/sign_in.dart';
  import 'package:prototype/gui/screens/text_viewer/text_viewer.dart';
  
  class ScreenWelcome extends StatelessWidget {
    final _controller = Controller();
  
    @override
    Widget build(BuildContext context) {
      return Container(
        child: SingleChildScrollView(
          child: Column(
            children: &lt;Widget&gt;[
              Image.asset('resources/zombiepaar.jpg',
                  width: 500, height: 500, fit: BoxFit.fitWidth),
              SizedBox(
                height: 20.0,
              ),
              RaisedButton(
                child: Text("Ein neues Konto erstellen"),
                onPressed: () =&gt; _controller.currentWidget = ScreenRegister(),
              ),
              SizedBox(
                height: 20.0,
              ),
              RaisedButton(
                child: Text("Mit bestehendem Konto einloggen"),
                onPressed: () =&gt; _controller.currentWidget = ScreenSignIn(),
              ),
              SizedBox(
                height: 20.0,
              ),
              Row(
                children: &lt;Widget&gt;[
                  GestureDetector(
                    child: Text(
                      "Nutzungsbedingungen",
                      style: TextStyle(
                          decoration: TextDecoration.underline,
                          color: Colors.blue),
                    ),
                    onTap: () =&gt; Navigator.push(
                        context,
                        MaterialPageRoute(
                          builder: (context) =&gt; ScreenTextViewer(
                              title: "Nutzungsbedingungen",
                              ressourceFileToLoad: 'resources/AGBs.txt'),
                        )),
                  ),
                  SizedBox(
                    width: 20.0,
                  ),
                  GestureDetector(
                    child: Text(
                      "Datenschutzrichtlinien",
                      style: TextStyle(
                          decoration: TextDecoration.underline,
                          color: Colors.blue),
                    ),
                    onTap: () =&gt; Navigator.push(
                      context,
                      MaterialPageRoute(
                        builder: (context) =&gt; ScreenTextViewer(
                            title: "Nutzungsbedingungen",
                            ressourceFileToLoad: 'resources/AGBs.txt'),
                      ),
                    ),
                  ),
                ],
              ),
            ],
          ),
        ),
      );
    }
  }
  
  </code></pre>
  
  <p>But when I ran my app I get the following runtime error:</p>
  
  <pre><code>D/FlutterActivityAndFragmentDelegate(21372): Executing Dart entrypoint: main, and sending initial route: /
  E/flutter (21372): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: Reading static variable '_instance@545324594' during its initialization
  E/flutter (21372): #0      Controller._instance (package:prototype/controller/conroller.dart:9:27)
  E/flutter (21372): #1      new Controller (package:prototype/controller/conroller.dart:17:27)
  E/flutter (21372): #2      new ScreenWelcome (package:prototype/gui/screens/welcome/welcome.dart:11:23)
  E/flutter (21372): #3      new Controller._internal (package:prototype/controller/conroller.dart:14:27)
  E/flutter (21372): #4      Controller._instance (package:prototype/controller/conroller.dart:9:50)
  E/flutter (21372): #5      Controller._instance (package:prototype/controller/conroller.dart:9:27)
  E/flutter (21372): #6      new Controller (package:prototype/controller/conroller.dart:17:27)
  E/flutter (21372): #7      new MyApp (package:prototype/main.dart:10:22)
  E/flutter (21372): #8      main (package:prototype/main.dart:6:23)
  E/flutter (21372): #9      _runMainZoned.&lt;anonymous closure&gt;.&lt;anonymous closure&gt; (dart:ui/hooks.dart:239:25)
  E/flutter (21372): #10     _rootRun (dart:async/zone.dart:1126:13)
  E/flutter (21372): #11     _CustomZone.run (dart:async/zone.dart:1023:19)
  E/flutter (21372): #12     _runZoned (dart:async/zone.dart:1518:10)
  E/flutter (21372): #13     runZoned (dart:async/zone.dart:1502:12)
  E/flutter (21372): #14     _runMainZoned.&lt;anonymous closure&gt; (dart:ui/hooks.dart:231:5)
  E/flutter (21372): #15     _startIsolate.&lt;anonymous closure&gt; (dart:isolate-patch/isolate_patch.dart:307:19)
  E/flutter (21372): #16     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:174:12)
  </code></pre>
  
  <p>Any idea how to fix this? I found nothing helpful with Google and I'm pretty new to dart and Flutter. Maybe I implemented the singleton class the wrong way?</p>
  
  <p>Edit: Mi main.dart file is</p>
  
  <pre class="lang-dart prettyprint-override"><code>import 'package:flutter/material.dart';
  
  // local file imports
  import 'package:prototype/controller/conroller.dart';
  
  void main() =&gt; runApp(MyApp());
  
  class MyApp extends StatelessWidget {
    // This widget is the root of the application.
    final controller = Controller(); // presenter in MVP design pattern
  
    @override
    Widget build(BuildContext context) {
      return MaterialApp(
        title: 'MyApp',
        home: controller.currentWidget, // controller decides the main widget
      );
    }
  }
  
  
  </code></pre>