No Directionality widget found
Asked Answered
T

6

92

I am trying to create a simple layout in flutter but keep getting this error:

The following assertion was thrown building Text("Deliver features faster", textAlign: center): No Directionality widget found.

I think the problem may be in the TextDirection class property, but I have been unable to find it.

Here is my code:

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

void main(){

  runApp( new MyHome());
}

class MyHome extends StatelessWidget{

  @override

  Widget build(BuildContext context) {
    // TODO: implement build
    return new Material(
      child: new Container(
        color: Colors.red,
        margin: const EdgeInsets.all(5.0),
        child: new Row(
          children: <Widget>[
            new Expanded(
              child: new Text('Deliver features faster', textAlign: TextAlign.center),
            ),
            new Expanded(
              child: new Text('Craft beautiful UIs', textAlign: TextAlign.center),
            ),
            new Expanded(
              child: new FittedBox(
                fit: BoxFit.contain, // otherwise the logo will be tiny
                child: const FlutterLogo(),
              ),
            ),
          ],
        )
      ),
    );
  }
}

Here is the full stack trace of the error:

The following assertion was thrown building Text("Deliver features faster", textAlign: center):
No Directionality widget found.
RichText widgets require a Directionality widget ancestor.
The specific widget that could not find a Directionality ancestor was:
  RichText(textAlign: center, softWrap: wrapping at box width, maxLines: unlimited, text: "Deliver
  features faster")
The ownership chain for the affected widget is:
  RichText ← Text ← Expanded ← Row ← DecoratedBox ← Padding ← Container ← DefaultTextStyle ←
  AnimatedDefaultTextStyle ← _InkFeatures-[GlobalKey#978b7 ink renderer] ← ⋯
Typically, the Directionality widget is introduced by the MaterialApp or WidgetsApp widget at the
top of your application widget tree. It determines the ambient reading direction and is used, for
example, to determine how to lay out text, how to interpret "start" and "end" values, and to resolve
EdgeInsetsDirectional, AlignmentDirectional, and other *Directional objects.
When the exception was thrown, this was the stack:
#0      debugCheckHasDirectionality.<anonymous closure> (package:flutter/src/widgets/debug.dart:223:7)
#1      debugCheckHasDirectionality (package:flutter/src/widgets/debug.dart:239:4)
#2      RichText.createRenderObject (package:flutter/src/widgets/basic.dart:4245:37)
#3      RenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4259:28)
#4      Element.inflateWidget (package:flutter/src/widgets/framework.dart:2890:14)
#5      Element.updateChild (package:flutter/src/widgets/framework.dart:2693:12)
#6      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3636:16)
#7      Element.rebuild (package:flutter/src/widgets/framework.dart:3478:5)
#8      ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3605:5)
#9      ComponentElement.mount (package:flutter/src/widgets/framework.dart:3600:5)
#10     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2890:14)
#11     Element.updateChild (package:flutter/src/widgets/framework.dart:2693:12)
#12     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3636:16)
#13     Element.rebuild (package:flutter/src/widgets/framework.dart:3478:5)
#14     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3605:5)
#15     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3600:5)
#16     ParentDataElement.mount (package:flutter/src/widgets/framework.dart:3938:16)
#17     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2890:14)
#18     MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4738:32)
#19     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2890:14)
#20     Element.updateChild (package:flutter/src/widgets/framework.dart:2693:12)
#21     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4633:14)
#22     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2890:14)
#23     Element.updateChild (package:flutter/src/widgets/framework.dart:2693:12)
#24     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4633:14)
#25     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2890:14)
#26     Element.updateChild (package:flutter/src/widgets/framework.dart:2693:12)
#27     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3636:16)
#28     Element.rebuild (package:flutter/src/widgets/framework.dart:3478:5)
#29     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3605:5)
#30     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3600:5)
#31     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2890:14)
#32     Element.updateChild (package:flutter/src/widgets/framework.dart:2693:12)
#33     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3636:16)
#34     Element.rebuild (package:flutter/src/widgets/framework.dart:3478:5)
#35     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3605:5)
#36     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3600:5)
#37     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2890:14)
#38     Element.updateChild (package:flutter/src/widgets/framework.dart:2693:12)
#39     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3636:16)
#40     Element.rebuild (package:flutter/src/widgets/framework.dart:3478:5)
#41     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3605:5)
#42     StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3752:22)
#43     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3600:5)
#44     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2890:14)
#45     Element.updateChild (package:flutter/src/widgets/framework.dart:2693:12)
#46     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4633:14)
#47     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2890:14)
#48     Element.updateChild (package:flutter/src/widgets/framework.dart:2693:12)
#49     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3636:16)
#50     Element.rebuild (package:flutter/src/widgets/framework.dart:3478:5)
#51     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3605:5)
#52     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3600:5)
#53     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2890:14)
#54     Element.updateChild (package:flutter/src/widgets/framework.dart:2693:12)
#55     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4633:14)
#56     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2890:14)
#57     Element.updateChild (package:flutter/src/widgets/framework.dart:2693:12)
#58     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3636:16)
#59     Element.rebuild (package:flutter/src/widgets/framework.dart:3478:5)
#60     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3605:5)
#61     StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3752:22)
#62     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3600:5)
#63     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2890:14)
#64     Element.updateChild (package:flutter/src/widgets/framework.dart:2693:12)
#65     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3636:16)
#66     Element.rebuild (package:flutter/src/widgets/framework.dart:3478:5)
#67     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3605:5)
#68     StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3752:22)
#69     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3600:5)
#70     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2890:14)
#71     Element.updateChild (package:flutter/src/widgets/framework.dart:2693:12)
#72     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3636:16)
#73     Element.rebuild (package:flutter/src/widgets/framework.dart:3478:5)
#74     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3605:5)
#75     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3600:5)
#76     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2890:14)
#77     Element.updateChild (package:flutter/src/widgets/framework.dart:2693:12)
#78     RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:852:16)
#79     RenderObjectToWidgetElement.mount (package:flutter/src/widgets/binding.dart:823:5)
#80     RenderObjectToWidgetAdapter.attachToRenderTree.<anonymous closure> (package:flutter/src/widgets/binding.dart:769:17)
#81     BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2205:19)
#82     RenderObjectToWidgetAdapter.attachToRenderTree (package:flutter/src/widgets/binding.dart:768:13)
#83     BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&RendererBinding&WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:657:7)
#84     runApp (package:flutter/src/widgets/binding.dart:699:7)
#85     main (/data/user/0/com.yourcompany.flutterproject/cache/flutter_projectENWZDI/flutter_project/lib/main.dart:6:3)
#86     _startIsolate.<anonymous closure> (dart:isolate-patch/dart:isolate/isolate_patch.dart:279)
#87     _RawReceivePortImpl._handleMessage (dart:isolate-patch/dart:isolate/isolate_patch.dart:165)
════════════════════════════════════════════════════════════════════════════════════════════════════
Another exception was thrown: No Directionality widget found.
Another exception was thrown: Horizontal RenderFlex with multiple children has a null textDirection, so the layout order is undefined.
Another exception was thrown: 'package:flutter/src/rendering/box.dart': Failed assertion: line 1446 pos 12: 'hasSize': is not true.
uid=10079(com.yourcompany.flutterproject) Thread-5 identical 2 lines
Another exception was thrown: 'package:flutter/src/rendering/box.dart': Failed assertion: line 1446 pos 12: 'hasSize': is not true.
Another exception was thrown: 'package:flutter/src/rendering/proxy_box.dart': Failed assertion: line 1564 pos 12: 'hasSize': is not true.
Thrashing answered 6/4, 2018 at 7:2 Comment(1)
Please post what you came up with and further explain what should be the expected behaviour you are looking forHedvah
G
107

flutter doesn't know whether the text is LTR or RTL, so you need to tell him the textDirection explicitly

Text("Hello", textDirection: TextDirection.ltr)

or you can just wrap the Text with a Directionality Widget

Directionality(
          textDirection: TextDirection.ltr,
          child: Text('Hello')

and the purpuse of that is :

A widget that determines the ambient directionality of text and text-direction-sensitive render objects.

And a Text widget in the scope of a MaterialApp widget does not need to be given an explicit writing direction because The default localization in the widgets and material libraries is LTR

Gilolo answered 6/4, 2018 at 9:47 Comment(0)
H
62

If you don't use MaterialApp you need to wrap your app into some widgets yourself

import 'dart:ui' as ui;
...
runApp(
    new MediaQuery(
        data: new MediaQueryData.fromWindow(ui.window),
        child: new Directionality(
            textDirection: TextDirection.rtl,
            child: new MyHome())))
Hilliary answered 6/4, 2018 at 7:25 Comment(5)
TextDirection.rtl what is use of this line'Thrashing
The Directionality widget needs to have a direction set. That's the main purpose of the widget.Duo
this ans is right so i put them correct but now i seen that person copy and past your answer.Thrashing
I am getting undefined name uiMoony
You need to add import 'dart:ui' as ui;Duo
M
45

You get this error, if you're not using MaterialApp or WidgetsApp at the root level of your tree hierarchy.

Solution:

void main() {
  runApp(
    MaterialApp(home: MyHome()), // use MaterialApp
  );
}

class MyHome extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold( // use Scaffold also in order to provide material app widgets
      body: Center(child: Text("Something")),
    );
  }
}
Mccusker answered 28/2, 2020 at 16:32 Comment(1)
Correct with the reason of my error, but unfortunately the solution did not work! Any updates if possible? If something has changed!Rhythmist
S
5

The problem is not that you have not wrapped your widgets into MaterialApp. As the documentation says this error occurs due to the nesting of the same kind of Widgets like nesting of Row into Row, Column into Column or Column into ListView so the problem arises as it becomes unbounded constraints for the Widget so unable to identify the direction.

So to avoid this kind of problems usage of Flexible or Expanded occurs which identifies the remaining space.

In your case, you can simply give crossAxisAlignment with a value of start to the Row widget.

For more information, visit this documentation page

Szeged answered 18/5, 2018 at 19:38 Comment(0)
L
2

This is not a solution to original question but for similar other scenario.

Totally agree with @CopsOnRoad's answer. Apart from that, there are scenarios you have to use MaterialApp.router option instead of MaterialApp. As an example, when we are using go_router plugin we may encounter this case.

For the happy path in the app development you will not get this issue with MaterialApp.router either. You will get this error if you try to write WidgetTest for this scenario. In that case one of the solutions is:

  testWidgets('Description', (widgetTester) async {
    await widgetTester.pumpWidget(const MaterialApp(
      home: YourScreen(),
    ));

    // rest of the code goes here
  });
Leakey answered 19/11, 2023 at 4:55 Comment(0)
A
0

but i think so it's scaffold issue you can't wrap in scaffold

Affright answered 4/4 at 7:54 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Airdry

© 2022 - 2024 — McMap. All rights reserved.