0

I just run the basic flutter code. I want to make stateful widget, only containing Text("hi") so I write my code like below

import 'package:flutter/material.dart';

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

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return Container(child: Text("hi");
  }
}

but I got error No Directionality widget found. RichText widgets require a Directionality widget ancestor

I can fix my error to add textDirection attribute, but I wonder when I use Text I didn't get that error even if I don't use that attribute. I just want to use only Text, Container

Minseo Kim
  • 13
  • 3
  • Wrap your text widget with a container and wrap that container in scaffold / Material App. Every flutter code must be wrapped in scaffold or material app. Surely it will give you desire output also learn Material app widget more in deep it will help you in future – amit.flutter May 10 '22 at 10:58

2 Answers2

0

i think its because you dont have MaterialApp

try it like this will work

void main() {
  runApp(const MyApp1());
}
class MyApp1 extends StatelessWidget {
  const MyApp1({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
     home: MyApp(),
    );
  }
}
class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return Container(child: Text("hi"));
  }
}
Abdulkarim
  • 547
  • 3
  • 13
0

Text needs a Directionality widget to provide information on whether the text is "left-to-right" or "right-to-left". Usually this is done (behind the scene) by MaterialApp widget. If you don't want to use that, you could provide one yourself, and specify the text direction.

Minimal code for a hello world:

import 'package:flutter/material.dart';

void main() {
  runApp(
    Directionality(
      textDirection: TextDirection.ltr,
      child: Text("Hello world."),
    ),
  );
}
WSBT
  • 33,033
  • 18
  • 128
  • 133