LateInitializationError in a simple Flutter's hello world code

Viewed 4997

There is following code:

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}
class MyApp extends StatelessWidget {
  const MyApp();
  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
          Text("Hello world")
        ]
    );
  }
}

Whenever trying to run this code, there is an error:

════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
LateInitializationError: _renderObject@20042623
...
E/flutter (14923): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 2734 pos 20: '_debugCurrentBuildTarget == context': is not true.
E/flutter (14923): #0      _AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:46:39)
E/flutter (14923): #1      _AssertionError._throwNew (dart:core-patch/errors_patch.dart:36:5)
E/flutter (14923): #2      BuildOwner.buildScope.<anonymous closure> (package:flutter/src/widgets/framework.dart:2734:20)
E/flutter (14923): #3      BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2738:12)
E/flutter (14923): #4      RenderObjectToWidgetAdapter.attachToRenderTree (package:flutter/src/widgets/binding.dart:1127:13)
E/flutter (14923): #5      WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:967:7)
E/flutter (14923): #6      WidgetsBinding.scheduleAttachRootWidget.<anonymous closure> (package:flutter/src/widgets/binding.dart:948:7)
E/flutter (14923): #7      _rootRun (dart:async/zone.dart:1182:47)
E/flutter (14923): #8      _CustomZone.run (dart:async/zone.dart:1093:19)
E/flutter (14923): #9      _CustomZone.runGuarded (dart:async/zone.dart:997:7)
E/flutter (14923): #10     _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1037:23)
E/flutter (14923): #11     _rootRun (dart:async/zone.dart:1190:13)
E/flutter (14923): #12     _CustomZone.run (dart:async/zone.dart:1093:19)
E/flutter (14923): #13     _CustomZone.bindCallback.<anonymous closure> (dart:async/zone.dart:1021:23)
E/flutter (14923): #14     Timer._createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:18:15)
E/flutter (14923): #15     _Timer._runTimers (dart:isolate-patch/timer_impl.dart:397:19)
E/flutter (14923): #16     _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:428:5)
E/flutter (14923): #17     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)
E/flutter (14923):

When the Text object is removed from the children of Column, then there is no problem. There is an option also to wrap the widgets with MaterialApp, but I was wondering why it doesn't work without that?

This is Dart 2.11.

2 Answers

Not sure why this is the error you get but the text widget is missing a Directionality ancestor widget that specifies if the text is left to right or right to left (MaterialApp also provides this)

So wrap the column in a Directionality

And file an issue (if it doesnt exist yet) on the flutter git repo for getting such an non descriptive error message

It is better to use ‍‍MaterialApp which provides the basic settings of the app for you:

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Training(),
    );
  }
}

class Training extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Column(
        children: <Widget>[
          Text("Hello world")
        ]
    );
  }
}

Related