I got an error Error: Entrypoint doesn't contain a main function Flutter, I tried to restart android studio but it did not work
Depends on what the program is support to do. There should be a main class that is initiated. As an example:
import 'package:flutter/material.dart';
import 'package:english_words/english_words.dart';
void main() => runApp(MyApp()); // initiate MyApp as StatelessWidget
// Main class
class MyApp extends StatelessWidget {
// creating main screen and building list of wordpairs
#override
This worked for me:
When new project is created then got the error "Entrypoint doesn't contain a main function Flutter".
To resolve this I followed below step
First make sure your flutter and dart sdk paths are properly configured.
You can re-include the files/project root folder.
Goto File -> Project Structure -> Module
And then add the root folder of the project by clicking + icon, then it will detect a flutter app again.
Build the project and check for the error.
add main function to your project. you might miss that
void main() => runApp(MyApp());
There's another case where this is possible, because of dart package file
delete all that .packages files, should get resolved.
Just do this
File -> Project structure -> Modules
From right select
Sources
form [Source, Paths, Dependencies] Tabs
Select Plus Icon -> Apply -> ok button
Make sure you use same name of project and pubspec.yaml file name Sometime it happens when we copy one pubspec file for another project.
in my case the launching function was named something else, renamed it to main, then it worked.
Add this if its missing in your main.dart file
void main() => runApp(MyApp());
This function tells Dart where the program starts, and it must be in the file that is considered the "entry point" for you program.
I cut some text above the main function and that included a closing } which made the main inside some class above it. That caused the above error. I put back the } above main and everything became ok.
Related
The bounty expires in 7 days. Answers to this question are eligible for a +50 reputation bounty.
Nerdy Bunz is looking for a canonical answer.
About a week ago, I created a new flutter project using:
flutter create -t app my_app
But when I opened the resulting project in the IDE, I was surprised to find the project has some key differences compared to what I'm used to:
there is a folder inside the lib folder, "src"
the main.dart does not contain the usual "counter app" and looks like this:
import 'package:flutter/material.dart';
import 'src/app.dart';
import 'src/settings/settings_controller.dart';
import 'src/settings/settings_service.dart';
void main() async {
// Set up the SettingsController, which will glue user settings to multiple
// Flutter Widgets.
final settingsController = SettingsController(SettingsService());
// Load the user's preferred theme while the splash screen is displayed.
// This prevents a sudden theme change when the app is first displayed.
await settingsController.loadSettings();
// Run the app and pass in the SettingsController. The app listens to the
// SettingsController for changes, then passes it further down to the
// SettingsView.
runApp(MyApp(settingsController: settingsController));
}
I tried creating other new projects and I verified it also happened when using "flutter create -t skeleton."
Now, after a week, it seems to have gone back to the old behavior.
Does anyone know the explanation for this?
I have a flutter local package, called "profile" and i have included and using this package from my main application as below
on pubspec.yaml file under dependencies i added the below code.
profile:
path: ./profile
Everything working well and good.
My issue is I want to navigate to MaterialApp -> Route when use click on a button in "Profile" package.
I tried to use Global navigation key
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
and from profile package when i use below code on button press
Navigator.pushNamed(context, '/', arguments: null);
It's doing nothing. Could you please help me with this.
Many thanks in advance.
It comes all of my Flutter Projects. Is the folder necessary or not ?
Do I delete the folder ?
Basically runApp() method(inside main.dart) contains a class and it should be same as defined in this tester.pumpWidget(....) method. If they don't match, it causes this error.
e.g
In your main.dart if runApp() method is having a class HomePage
runApp(const HomePage()); then inside widget_test.dart file it should be like await tester.pumpWidget(const HomePage());
I got the same error and when i move my mouse on it,it shows that MyApp() links to another project,e.g:
and it links to library'package:try_base_flutter/main.dart',then I found every dart file in the lib links to the try_base_flutter.So I search the key word "try_base_flutter",finally I found the wrong code in the pubspec.yaml's first line
,I write the name: try_base_flutter,because I copy the file from the project try_base_flutter,so just change the name,my problem will be fine.
Remove const. like await tester.pumpWidget(MyApp());
I am trying to force my Flutter app to only use Portrait mode. My main() method looks like this.
void main() {
SystemChrome.setSystemUIOverlayStyle(uiOverlayStyle);
SystemChrome.setPreferredOrientations(ALLOWED_ORIENTATIONS)
.then((_) => runApp(MyApp()));
}
Here is the definition of ALLOWED_ORIENTATIONS:
const List<DeviceOrientation> ALLOWED_ORIENTATIONS = [
DeviceOrientation.portraitUp
];
I just separated all the settings and such to another file to make modifying them later on easier.
When I run this code, I get the following error.
[VERBOSE-2:ui_dart_state.cc(157)] Unhandled Exception:
ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized.
If you're running an application and need to access the binary messenger before `runApp()`
has been called (for example, during plugin initialization), then you need to explicitly
call the `WidgetsFlutterBinding.ensureInitialized()` first.
It looks like there is some race condition that's failing. I am just not sure what is causing it. Removing the SystemChrome.setPreferredOrientations() line makes the app compile as expected. So, I am thinking the error has something to do with that line.
Any advice?
Like the error says
If you're running an application and need to access the binary
messenger before runApp() has been called (for example, during
plugin initialization), then you need to explicitly call the
WidgetsFlutterBinding.ensureInitialized() first.
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
SystemChrome.setSystemUIOverlayStyle(uiOverlayStyle);
await SystemChrome.setPreferredOrientations(ALLOWED_ORIENTATIONS);
runApp(MyApp());
}
I just began learning flutter and built the sample app present in Building Layout tutorial.
In the source code it's suggested to un-comment two lines, to see the visual debug lines, but so far no luck.
import 'package:flutter/material.dart';
// Uncomment lines 7 and 10 to view the visual layout at runtime.
//import 'package:flutter/rendering.dart' show debugPaintSizeEnabled;
void main() {
//debugPaintSizeEnabled = true;
runApp(new MyApp());
}
What I have tried?
Hot Reload
Full Restart
Setting other debug variables to true:
debugPaintPointersEnabled =
debugPaintBaselinesEnabled =
debugPaintLayerBordersEnabled =
debugRepaintRainbowEnabled = true;
, which I read from Docs. They worked fine.
My Setup?
Visual Studio Code
No Dart 2 Preview Mode
Flutter Beta
I use an Android Mobile Hardware, not Virtual (Moto G5)
Question: How to make the visual debugger work?
I had exactly the same problem and the only solution i found, is to toggle debug painting from VSCode command palette.
Flutter: Toggle Debug Painting
You can also make it works using the rendering library.
First you need to import it
import 'package:flutter/rendering.dart';
Then, set debugPaintSizeEnabled to true in the main method of your application or in a widget's build method
void main() {
debugPaintSizeEnabled=true;
runApp(MyApp());
}
You need to fully restart your application to apply effects
Here's the official documentation.
Add import statements :
import 'dart:developer';
import 'package:flutter/rendering.dart';
Then in the build add the debugPaintSizeEnabled=true; like :
Widget build(BuildContext context) {
debugPaintSizeEnabled=true;
It's not necessary import anything in VSCode, just:
Open command palette by Ctrl+Shift+P (Cmd for mac)
Type Flutter: Toggle Debug Painting and click on it: example.
I think you need import 'package:flutter/rendering.dart';
UPDATE
The following steps work on both android device and android
virtual device if you are working with ANDROID STUDIO. It works only
on Android virtual device if you are working wih VSCode
I was following the same tutorial recently to learn all about the layout elements in Flutter.
To enable visual layout at runtime, what I did was quite straightforward -
First
I added import 'package:flutter/rendering.dart' show debugPaintSizeEnabled; at the top of my main.dart file
Next
I added debugPaintSizeEnabled = true; to my main() method
void main() {
debugPaintSizeEnabled = true;
runApp(new MyApp());
}
Finally
I performed a full restart of my app to reflect all the changes. It doesn't reflect changes if you perform a hot reload.
Hope this helps.
In the terminal press 'p'
To toggle the display of construction lines (debugPaintSizeEnabled), press "p".
(this is the easiest option!)
import 'package:flutter/material.dart';
void main() {
debugPaintSizeEnabled = true;
runApp(new MyApp()); }
Open command palette by CTRL + SHIFT + P (for window),
type Flutter: Toggle Debug Painting and click on it.
I had the same issue, wasn't able to see any debug information.
Fixed it by running in debug mode instead of profile or release.
Maybe this will help someone.