No Directionality widget found. InkWell widgets require a Directionality widget ancestor
Asked Answered
G

3

7

I am totally new to flutter and my first Steps already caused errors I cant solve by my own or searching this website..

I want to create a Button but get an Execption of missing Directionality as I understand. The only widget which needs the textDirection is the Text Widget, but it has ist, so whats wrong here? A Full Start also already tried several times incl. new emulator..

This is my code:

import 'package:flutter/material.dart';

void main(){
  runApp(
    new MyStatefullWidget()
  ) ;
}


class MyStatefullWidget extends StatefulWidget {

  @override
  _MyStatefullWidgetState createState() => _MyStatefullWidgetState();
}

class _MyStatefullWidgetState extends State<MyStatefullWidget> {

  @override
  Widget build(BuildContext context) {
    return Container(
      color:Colors.red,
      child: Center(
        child: RaisedButton(
          child: Text("klick mich", textDirection: TextDirection.ltr),
          onPressed: (){
            print("Hi");
          },
        ),
      ),
    );
  }
}

and this is the Error message:

I/flutter ( 4763): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter ( 4763): The following assertion was thrown building InkWell(gestures: [tap], clipped to BoxShape.rectangle,
I/flutter ( 4763): dirty, state: _InkResponseState<InkResponse>#43345):
I/flutter ( 4763): No Directionality widget found.
I/flutter ( 4763): InkWell widgets require a Directionality widget ancestor.
I/flutter ( 4763): The specific widget that could not find a Directionality ancestor was:
I/flutter ( 4763):   InkWell(gestures: [tap], clipped to BoxShape.rectangle)
I/flutter ( 4763): The ownership chain for the affected widget is:
I/flutter ( 4763):   InkWell ← DefaultTextStyle ← AnimatedDefaultTextStyle ← _InkFeatures-[GlobalKey#64251 ink
I/flutter ( 4763):   renderer] ← NotificationListener<LayoutChangedNotification> ← CustomPaint ← _ShapeBorderPaint ←
I/flutter ( 4763):   PhysicalShape ← _MaterialInterior ← Material ← ⋯
I/flutter ( 4763): Typically, the Directionality widget is introduced by the MaterialApp or WidgetsApp widget at the
I/flutter ( 4763): top of your application widget tree. It determines the ambient reading direction and is used, for
I/flutter ( 4763): example, to determine how to lay out text, how to interpret "start" and "end" values, and to resolve
I/flutter ( 4763): EdgeInsetsDirectional, AlignmentDirectional, and other *Directional objects.
I/flutter ( 4763): 
I/flutter ( 4763): When the exception was thrown, this was the stack:
I/flutter ( 4763): #0      debugCheckHasDirectionality.<anonymous closure> (package:flutter/src/widgets/debug.dart:241:7)
I/flutter ( 4763): #1      debugCheckHasDirectionality (package:flutter/src/widgets/debug.dart:257:4)
I/flutter ( 4763): #2      InkResponse.debugCheckContext (package:flutter/src/material/ink_well.dart:376:12)
I/flutter ( 4763): #3      _InkResponseState.build (package:flutter/src/material/ink_well.dart:554:19)
I/flutter ( 4763): #4      StatefulElement.build (package:flutter/src/widgets/framework.dart:3809:27)
I/flutter ( 4763): #5      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3721:15)
I/flutter ( 4763): #6      Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 4763): #7      ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3701:5)
I/flutter ( 4763): #8      StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3848:11)
I/flutter ( 4763): #9      ComponentElement.mount (package:flutter/src/widgets/framework.dart:3696:5)
I/flutter ( 4763): #10     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
I/flutter ( 4763): #11     Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
I/flutter ( 4763): #12     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
I/flutter ( 4763): #13     Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 4763): #14     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3701:5)
I/flutter ( 4763): #15     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3696:5)
I/flutter ( 4763): #16     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
I/flutter ( 4763): #17     Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
I/flutter ( 4763): #18     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
I/flutter ( 4763): #19     Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 4763): #20     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3701:5)
I/flutter ( 4763): #21     StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3848:11)
I/flutter ( 4763): #22     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3696:5)
I/flutter ( 4763): #23     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
I/flutter ( 4763): #24     Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
I/flutter ( 4763): #25     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4860:14)
I/flutter ( 4763): #26     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
I/flutter ( 4763): #27     Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
I/flutter ( 4763): #28     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
I/flutter ( 4763): #29     Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 4763): #30     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3701:5)
I/flutter ( 4763): #31     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3696:5)
I/flutter ( 4763): #32     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
I/flutter ( 4763): #33     Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
I/flutter ( 4763): #34     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4860:14)
I/flutter ( 4763): #35     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
I/flutter ( 4763): #36     Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
I/flutter ( 4763): #37     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
I/flutter ( 4763): #38     Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 4763): #39     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3701:5)
I/flutter ( 4763): #40     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3696:5)
I/flutter ( 4763): #41     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
I/flutter ( 4763): #42     Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
I/flutter ( 4763): #43     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4860:14)
I/flutter ( 4763): #44     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
I/flutter ( 4763): #45     Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
I/flutter ( 4763): #46     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
I/flutter ( 4763): #47     Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 4763): #48     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3701:5)
I/flutter ( 4763): #49     StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3848:11)
I/flutter ( 4763): #50     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3696:5)
I/flutter ( 4763): #51     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
I/flutter ( 4763): #52     Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
I/flutter ( 4763): #53     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
I/flutter ( 4763): #54     Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 4763): #55     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3701:5)
I/flutter ( 4763): #56     StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3848:11)
I/flutter ( 4763): #57     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3696:5)
I/flutter ( 4763): #58     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
I/flutter ( 4763): #59     Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
I/flutter ( 4763): #60     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4860:14)
I/flutter ( 4763): #61     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
I/flutter ( 4763): #62     Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
I/flutter ( 4763): #63     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4860:14)
I/flutter ( 4763): #64     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
I/flutter ( 4763): #65     Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
I/flutter ( 4763): #66     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4860:14)
I/flutter ( 4763): #67     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
I/flutter ( 4763): #68     Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
I/flutter ( 4763): #69     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
I/flutter ( 4763): #70     Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 4763): #71     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3701:5)
I/flutter ( 4763): #72     StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3848:11)
I/flutter ( 4763): #73     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3696:5)
I/flutter ( 4763): #74     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
I/flutter ( 4763): #75     Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
I/flutter ( 4763): #76     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
I/flutter ( 4763): #77     Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 4763): #78     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3701:5)
I/flutter ( 4763): #79     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3696:5)
I/flutter ( 4763): #80     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
I/flutter ( 4763): #81     Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
I/flutter ( 4763): #82     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4860:14)
I/flutter ( 4763): #83     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
I/flutter ( 4763): #84     Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
I/flutter ( 4763): #85     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4860:14)
I/flutter ( 4763): #86     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
I/flutter ( 4763): #87     Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
I/flutter ( 4763): #88     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
I/flutter ( 4763): #89     Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 4763): #90     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3701:5)
I/flutter ( 4763): #91     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3696:5)
I/flutter ( 4763): #92     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
I/flutter ( 4763): #93     Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
I/flutter ( 4763): #94     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
I/flutter ( 4763): #95     Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 4763): #96     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3701:5)
I/flutter ( 4763): #97     StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3848:11)
I/flutter ( 4763): #98     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3696:5)
I/flutter ( 4763): #99     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
I/flutter ( 4763): #100    Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
I/flutter ( 4763): #101    RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:909:16)
I/flutter ( 4763): #102    RenderObjectToWidgetElement.mount (package:flutter/src/widgets/binding.dart:880:5)
I/flutter ( 4763): #103    RenderObjectToWidgetAdapter.attachToRenderTree.<anonymous closure> (package:flutter/src/widgets/binding.dart:826:17)
I/flutter ( 4763): #104    BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2266:19)
I/flutter ( 4763): #105    RenderObjectToWidgetAdapter.attachToRenderTree (package:flutter/src/widgets/binding.dart:825:13)
I/flutter ( 4763): #106    _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding&WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:712:7)
I/flutter ( 4763): #107    runApp (package:flutter/src/widgets/binding.dart:756:7)
I/flutter ( 4763): #108    main (package:flutter_app/main.dart:4:3)
I/flutter ( 4763): #109    _startIsolate.<anonymous closure> (dart:isolate/runtime/libisolate_patch.dart:289:19)
I/flutter ( 4763): #110    _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:171:12)
I/flutter ( 4763): ════════════════════════════════════════════════════════════════════════════════════════════════════
Ghat answered 25/12, 2018 at 16:20 Comment(0)
P
16

You have to add a MaterialApp widget before container, like this:

class _MyStatefullWidgetState extends State<MyStatefullWidget> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Container(
        color: Colors.red,
        child: Center(
          child: RaisedButton(
            child: Text("klick mich", textDirection: TextDirection.ltr),
            onPressed: () {
              print("Hi");
            },
          ),
        ),
      ),
    );
  }
}
Prig answered 25/12, 2018 at 16:31 Comment(0)
F
0

I had this same error and to add more info, was able to resolve by looking at the error message, but the short of it is we can use MaterialApp or WidgetsApp at the top of the widget tree (as shown above):

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════

The following assertion was thrown building InkWell(gestures: [tap], clipped to BoxShape.rectangle, dirty, state: _InkResponseState#1b61e):

No Directionality widget found.

InkWell widgets require a Directionality widget ancestor.

The specific widget that could not find a Directionality ancestor was: InkWell

The ownership chain for the affected widget is: "InkWell ← DefaultTextStyle ← AnimatedDefaultTextStyle ← _InkFeatures-[GlobalKey#3bd76 ink renderer] ← NotificationListener ← CustomPaint ← _ShapeBorderPaint ← PhysicalShape ← _MaterialInterior ← Material ← ⋯"

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.

Faunus answered 11/6, 2020 at 0:11 Comment(0)
M
0

An Answer in 2024

For those who are not using Material design in your flutter app and encountering "No Directionality widget found" error:

As a fix you can wrap your Root Widget with Directionality widget at the top level of your widget tree.

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Directionality(
      textDirection: TextDirection.ltr, // Set to ltr or rtl based on your needs
      child: MyNonMaterialWidget(),
    );
  }
}

class MyNonMaterialWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return const Center(
      child: Text("Foo bar"),
    );
  }
}

#NonMaterialApps #Flutter

Mamelon answered 1/9, 2024 at 17:50 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.