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.
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,
)
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)
),
),
),
);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With