6

I am getting below exception while running flutter code in android studio.

I/flutter ( 5360): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════ I/flutter ( 5360): The following assertion was thrown building Text("xyz"): I/flutter ( 5360): No Directionality widget found. I/flutter ( 5360): RichText widgets require a Directionality widget ancestor. I/flutter ( 5360): The specific widget that could not find a Directionality ancestor was: I/flutter ( 5360): RichText(softWrap: wrapping at box width, maxLines: unlimited, text: "xyz") I/flutter ( 5360): The ownership chain for the affected widget is: I/flutter ( 5360): RichText ← Text ← Center ← Container ← [root] I/flutter ( 5360): Typically, the Directionality widget is introduced by the MaterialApp or WidgetsApp widget at the I/flutter ( 5360): top of your application widget tree. It determines the ambient reading direction and is used, for I/flutter ( 5360): example, to determine how to lay out text, how to interpret "start" and "end" values, and to resolve I/flutter ( 5360): EdgeInsetsDirectional, AlignmentDirectional, and other *Directional objects.

Code snippet:

import 'package:flutter/material.dart';

void main() {
  runApp(
    Container(child: Center(child: Text("xyz"))),
  );
}

I am not sure why this exception says

I/flutter ( 5360): Typically, the Directionality widget is introduced by the MaterialApp or WidgetsApp widget at the ...

Because in flutter everything is a widget, so any widget should work for us.

0

6 Answers 6

16

If you are using a material widget component, like Scaffold, Material, you don't need to specify textDirection in your Text widget because Directionality is implicitly provided in these cases. So, a good solution would be to use:

Scaffold(body: Text("xyz"))

But if you don't use it, you will have to

Text(
    'xyz',
    textDirection: TextDirection.ltr, 
)
Sign up to request clarification or add additional context in comments.

Comments

5

If you are using Directionality constructor your problem will resolve

Widget build(BuildContext context) {
    return Directionality(
      textDirection: TextDirection.ltr,
      child: Container(
        color: myColor,
        child: Center(
          child: RaisedButton(
            onPressed: () {
              print('You pressed me');
              changeColor();
            },
            child: Text('Click', textDirection: TextDirection.ltr)
          ),
        ),
      ),
    );
  }

Comments

4

Text widget needs to know whether it should display text left to right or right to left. There is no default. This information can be provided in 2 ways:

  • by nesting it in Directionality widget (as mentioned in the error message, typically MaterialApp or WidgetsApp)
  • explicitly, as an argument to the constructor:
Text(
    'Hello World',
    textDirection: TextDirection.ltr,
)

Second approach clearly is unmanageable in the long run.

Comments

2

late but I hope it can help. I don't use any predefined root widget too. So I wrapped my App in Directionality widget in main function and flutter stopped complaining:

my code:

void main() {
  runApp(
    new Directionality(textDirection: TextDirection.ltr, child: MyApp())
    );
}

Comments

0

A little more elaboration on what's happening internally.

When a Text widget is used without the specified style, it tries to inherit the style from the parents automatically. Here's the first few lines of the build method of Text Widget.

  @override
  Widget build(BuildContext context) {
    final DefaultTextStyle defaultTextStyle = DefaultTextStyle.of(context);
    TextStyle? effectiveTextStyle = style;
    if (style == null || style!.inherit)
      effectiveTextStyle = defaultTextStyle.style.merge(style);
    if (MediaQuery.boldTextOverride(context))
      effectiveTextStyle = effectiveTextStyle!.merge(const TextStyle(fontWeight: FontWeight.bold));

Look at the line - final DefaultTextStyle defaultTextStyle = DefaultTextStyle.of(context);

Now if a material widget is used in the widget tree before the Text Widget appears, they would have already set the default text style which Text widget automatically inherits from the Build Context. If none of the material widgets were used before Text, then we would need to specify the text style explicitly.

Comments

0

Ensure that the MaterialApp or CupertinoApp widget is correctly placed in your app. These widgets automatically include a Directionality widget.no need to add Directionality bye manually.

   // enter code here

void main() {
    testWidgets('home testwidget.', (tester) async {
        await tester.pumpWidget(MaterialApp(home: const MyHomePage()));// only add materialapp manually
        final ctr = find.text('0');
        expect(ctr, findsOneWidget);
    });
}

you try these easy steps--enter code here

  1. click on Widgetname in my case MyhomePage() and wrap with MaterialApp.
  2. change material app's child to home.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.