Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: No Directionality widget found

Tags:

flutter

dart

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.

like image 290
Jitesh Mohite Avatar asked Nov 30 '22 13:11

Jitesh Mohite


2 Answers

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, 
)
like image 187
CopsOnRoad Avatar answered Dec 03 '22 02:12

CopsOnRoad


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)
          ),
        ),
      ),
    );
  }
like image 29
Naredhar Chiluka Avatar answered Dec 03 '22 03:12

Naredhar Chiluka