No Directionality widget found ; I want to use only Container and Text - flutter

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

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"));
}
}

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."),
),
);
}

Related

three clases use setState callback to update var on other class

I have next thre classes:
Shortly the scenario: have two classes MyApp1, MyApp2 and MyApp3,
MyApp3 updates the var count with callback (indeed updated), I would like to print it (count) on MyApp2, how can I setState also on MyApp2 so it will take effect?
my_app1.dart
class MyApp1 extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
#override
State<MyApp1> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final int count = 0;
void callbakc() {
setState(() {
count++;
});
}
#override
Widget build(BuildContext context) {
return Container(
Row(
children:[
MyApp2(),
MyApp3(callback: callback), //suppose I have button on MyApp3, pressed it, and indded count increased by 1
]
),
);
}
}
my_app2.dart
class MyApp2 extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
#override
State<MyApp2> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
#override
Widget build(BuildContext context) {
return Container(
child: Text(count), how can I see the update count? from MyApp1?
);
}
}
my_app3.dart
class MyApp3 extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
#override
State<MyApp3> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
#override
Widget build(BuildContext context) {
return Container();
}
}
use state management to do this. for example GetX. Type GetX in pub.dev and search.
Avoid setState. because it will increase the workload of your program.
For the answer to your question, see the link below.
Flutter Back button with return data
Just pass the count parameter to MyApp2 so when your callback function run then it again render MyApp2 and update the count on MyApp2 also
here is an example -
Code in MyApp1
MyApp2(count:count),
MyApp2 Code
class MyApp2 extends StatefulWidget {
const MyApp({Key? key,this.count}) : super(key: key);
final int count;
#override
State<MyApp2> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
#override
Widget build(BuildContext context) {
return Container(
child: Text($`enter code here count), how can I see the update count? from MyApp1?
);
}
}

How to use flutter negation operator in stateful widget?

In normal approach to use negation operator is like this
isValid = !isValid
but this approach is not working inside stateful widget constructor variable like
widget.isValid = !widget.isValid
How to make this second approach valid .
You are adding the negation to just widget, It needs to be added to widget.isValid . Hence wrap it within brackets to work.
Change !widget.isValid to (!widget.isValid)
Example:
class MyApp extends StatefulWidget {
MyApp({Key? key}) : super(key: key);
bool value = true;
#override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
#override
void initState() {
super.initState();
}
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: SafeArea(
child: Scaffold(
body: Center(child: Text((!widget.value).toString())),
),
),
);
}
}
Output:

org-dartlang-app:/web_entrypoint.dart:16:22: Error: Undefined name 'main'. return (entrypoint.main as _NullaryFunction)();

hi i try to open flutter app on chrome and that what happen.org-dartlang-app:/web_entrypoint.dart:16:22: Error: Undefined name 'main'.
return (entrypoint.main as _NullaryFunction)();
my code:`
import 'package:flutter/material.dart';
class hellp extends StatefulWidget {
const hellp({ Key? key }) : super(key: key);
#override
State<hellp> createState() => _hellpState();
}
class _hellpState extends State<hellp> {
#override
Widget build( BuildContext context) {
return Container(
);
}
}
Just faced this error message while attempting to test a code snippet I found online. Mine was with a stateless widget though.
I solved it by adding the following code after the import statements:
import 'package:flutter/material.dart';
void main() { //add this
runApp(const MyApp());
}
//using stateless widget here.
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: const MyHomePage(),
);
}
Hope this helps :)

what is the difference between creating widget using widget class, and creating class widget that extend statelessWidget?

so i have this code:
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Home()
);
}
}
Widget Home() {
return Container(child: Text('aa'),);
}
class Home2 extends StatelessWidget {
const Home2({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Container(child: Text('aa'));
}
}
what is the difference between Home() and Home2()? will they work the same or they have something special?
Usually 'Home' function is called 'Helper method'.
Here is a official flutter video that exlain difference between helper method and widget.
https://www.youtube.com/watch?v=IOyq-eTRhvo&ab_channel=Flutter
The widgets in helper method is not detected in widget tree structure
performance: Helper method is all rebuilded when it needs refresh.
When the code is changed in Helper method, hot reload is not worked
...and so on..

Flutter UpperCamelCase

I'm new to flutter and I made my main.dart route to my splashscreen. In my main.dart, it accepted the name splashscreen() but in my splashscreen.dart it kept flagging the error "Name types using UpperCamelCase" when I named my class splashscreen.
Here is my main.dart
import 'package:flutter/material.dart';
import 'Routes/splashscreen.dart';
void main (){
runApp(MyApp());
}
class MyApp extends StatelessWidget {
MyApp ({Key? key}) : super(key: key)
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: "DoyarPay",
home: splashscreen(),
);
}
}
Here is my splashscreen.dart
import 'package:flutter/material.dart';
class splashscreen extends StatefulWidget {
const splashscreen({ Key? key }) : super(key: key);
#override
_splashscreenState createState() => _splashscreenState();
}
class _splashscreenState extends State<splashscreen> {
#override
Widget build(BuildContext context) {
return Container(
);
}
}
On the splashscreen.dart it gives the "Name types using UpperCamelCase" error.
Can anyone help me out on this.
Thanks
It is "accepted" but the warning indicates that you should follow some specific naming conventions. You should definetely check here which has more details about Effective Dart.
Widgets always start with uppercase letter so change your splashscreen to Splashscreen, not only widgets but classes as well
import 'package:flutter/material.dart';
import 'Routes/splashscreen.dart';
void main (){
runApp(MyApp());
}
class MyApp extends StatelessWidget {
MyApp ({Key? key}) : super(key: key)
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: "DoyarPay",
home: Splashscreen(),
);
}
}
And your splashscreen.dart
import 'package:flutter/material.dart';
class Splashscreen extends StatefulWidget {
const Splashscreen({ Key? key }) : super(key: key);
#override
_SplashscreenState createState() => _SplashscreenState();
}
class _SplashscreenState extends State<Splashscreen> {
#override
Widget build(BuildContext context) {
return Container(
);
}
}
Classes in dart has to be in UpperCamelCase.
Probably the linter doesn't detect the "error" (is not an error, you are just violating a convention) in your main.dart but it does during class declaration.
To follow dart standards just change the code using UpperCamelCase consistently:
return MaterialApp(
debugShowCheckedModeBanner: false,
title: "DoyarPay",
home: SplashScreen(),
);
import 'package:flutter/material.dart';
class SplashScreen extends StatefulWidget {
const SplashScreen({ Key? key }) : super(key: key);
#override
_SplashScreenState createState() => _SplashScreenState();
}
class _SplashScreenState extends State<SplashScreen> {
#override
Widget build(BuildContext context) {
return Container(
);
}
}
For class name in dart, suggest using CamelCase means it will start with an uppercase letter and the next word's 1st letter will be uppercase as well.
In your case splashscreen which starts with s which is lowercase. Appropriate will be SplashScreen.
For file name, we use snake_case. In this case splashscreen.dart will be splash_screen.dart
Learn more about
effective-dart
Camel case vs. snake case