I am new to flutter, created a demo app. just using stateless widget
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return new MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
title: Text("Hello my app"),
),
body: HelloRectangle()),
);
}
}
class HelloRectangle extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
color: Colors.blueAccent,
);
}
}
But it shows the error :
Error -32601 received from application: Method not found
Reloaded 0 of 432 libraries in 525ms.
Is it always necessary name the class MyApp , as I could not run it if its name is changed.I am running flutter using VS CODE, and running "start debugging option"
Related
I have reinstalled the flutter and Android Studio to my device and after that Scaffold widget is not working. It says "This method 'Scaffold' isn't defined for the type 'MyApp' "
add wrap Scaffold with MaterialApp widget
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Welcome to Flutter',
home: Scaffold(
appBar: AppBar(
title: const Text('Welcome to Flutter'),
),
body: const Center(
child: Text('Hello World'),
),
),
);
}
}
I am brand new to flutter and programming in general -so please bear with me-, so I am taking a course on udemy and I stumbled with this error (The constructor being called isn't a const constructor)
This is the code :
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar:AppBar(
title: Text('My First App'),
),
body: Text('This is my default text'),
),
);
}
}
Try below code hope its help to you. remove const keyword
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('My First App'),
),
body: Text('This is my default text'),
),
);
}
}
Your result screen->
At first my problem was that MyApp isn't defined and then I deleted the page that was for testing(?)... Now my problem is - A build function returned null. Offending widget is MyApp.
How do I get that page back? Do you have the code for it?
Here's the code I'm trying to run on an ios simulator, it's not right but it used to be white, now it has a red screen with that text
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
MaterialApp(
home: Scaffold(
body: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [Color(0x50658C), Color(0x2D4067)],
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
),
),
),
),
);
}
}
I'm very new at this
This error happens because you missed a return. In your case you forgot to return the MaterialApp.
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
// Missing return
return MaterialApp(
...
);
}
}
Write below code in main.dart:
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
final appTitle = 'Drawer Demo';
#override
Widget build(BuildContext context) {
return MaterialApp(title: appTitle, theme: ThemeData.light(),
home: LoginScreen(), // Do not specify Home when specifying routes.
);
}
}
class LoginScreen extends StatefulWidget {
#override
_LoginScreenState createState() => _LoginScreenState();
}
class _LoginScreenState extends State<LoginScreen> {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child: Text('Done'),
),
);
}
}
And run project....
I'm getting error in this code. Any help?
import 'package:flutter/material.dart';
import './homepage.dart';
void main() => (MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: "Chat",
home: HomePage(),
);
}
}
In HomePage.dart file
import 'package:flutter/material.dart';
class HomePage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
"Chat"
),
),
);
}
}
In Debug Console it shows:
Restarted application in 2,271ms.
Error -32601 received from application: Method not found
Reloaded 0 of 496 libraries in 237ms
The issue is with your code. Your syntax for the main method doesn't make sense for running a flutter app. You likely intended to call runApp on the widget within main.
Changed main:
import 'package:flutter/material.dart';
import './homepage.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: "Chat",
home: HomePage(),
);
}
}
`
I'm new to flutter and I'm testing Provider and can't figure out why doing this works (by work i mean it shows a list in the appbar):
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return ChangeNotifierProvider<Data>(
builder: (context)=> Data(),
child: MaterialApp(
home: Scaffold(
appBar: AppBar(
title: CustomText(),
),
),
),
);
}
}
With a CustomText class that does practically nothing:
class CustomText extends StatelessWidget{
#override
Widget build(BuildContext context) {
return Text(Provider.of<Data>(context).texts.tostring());
}
}
But this other thing throws a - Could not find the correct Provider above this MyApp Widget - Error:
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return ChangeNotifierProvider<Data>(
builder: (context)=> Data(),
child: MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text(Provider.of<Data>(context).texts.toString()),
),
),
),
);
}
}
The Data class is :
class Data with ChangeNotifier{
List<String> _texts = ['Text 1', 'Text 2', 'Text 3', 'Text 4',];
get texts {
return _texts;
}
void deleteText(int index){
this._texts.removeAt(index);
notifyListeners();
}
void addText(String text){
this._texts.add(text);
notifyListeners();
}
}
I just can not see what is the difference or why that matters. Shouldn't that code be equivalent? Any insight will be much appreciated.
The difference is that in the CustomText case, context is from its parent widget which is MyApp, whereas in the second case, context is from MyApp's parent. Since your Provider is implemented inside MyApp, it cannot find the provider if you use MyApp's parent's context (the second case)