Flutter: No Directionality widget found

Issue

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.

Solution

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, 
)

Answered By – CopsOnRoad

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published