debugPaintSizeEnabled is not working in Flutter - flutter

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.

Related

Flutter web build - blank page and stops on web_entrypoint.dart file

I'm trying to build a Flutter web fr my app project.
After passing "Flutter Create ." and trying to run in Chrome it shows a blank page and stops on the web_entrypoint.dart file.
Anyone knows what could be wrong?
perhaps you want to remove the Future<void> before main
Future<void> main() async{} //<--- remove Future<void>
// do
void main() async{
/* body */
}

Flutter only import library in debug mode

I am using Android 11 Wireless Debugging to develop my app. Whenever the device automatically locks itself, it takes a while to re-establish the connection for hot reloading.
To overcome this I am using wakelock, which I only need to use if my app is in debug mode, not in release mode.
In lib/main.dart I have the following code:
import 'package:flutter/foundation.dart' as Foundation;
import 'package:wakelock/wakelock.dart';
...
void main() {
if (Foundation.kDebugMode) {
Wakelock.enable();
}
runApp(App());
}
As you can see the wakelock package is only used if the app is running in debug mode.
Is there a way to only import wakelock if the app is running in debug mode?
Tested it as
pubspec.yaml
dev_dependencies:
wakelock: ^0.2.1+1
Usage
import 'package:flutter/foundation.dart';
import 'package:wakelock/wakelock.dart';
import 'package:flutter/material.dart';
main() async {
WidgetsFlutterBinding.ensureInitialized();
if (kDebugMode) {
print('activating wakelock in debug');
Wakelock.enable();
}
runApp(App());
}
Sidenote:
If all you need is the device to stop locking itself after some time then try increasing the sleep delay under the Display setting on the device itself, or use a setting in developer options called Stay awake while charging which allows the device to stay on forever while charging.

Disable screenshot in flutter

How to disable screenshot in flutter? I have Kotlin file instead of MainActivity.java file. Please suggest full code with path where I need to change for disable screenshot in my app.
package com.quotster.untitled
import io.flutter.embedding.android.FlutterActivity
class MainActivity: FlutterActivity() {
}
If you want to do that in Android only you can use flutter_windowmanager and add FLAG_SECURE.
await FlutterWindowManager.addFlags(FlutterWindowManager.FLAG_SECURE);

is it possible to have flutter desktop app always on top on mac os?

I tried searching for any documentation / search results on this, but I couldn't find any, is it possible to have a macOS desktop app created with flutter always on top on macOS?
The default macOS Flutter application is just a standard NSWindow containing a Flutter view, so you would make it always on top the same way you would any other macOS application window, using NSWindowLevel.
There are two ways you can get to the window in your Flutter app:
Drectly in MainFlutterWindow.swift, if your want it to always be on top.
Via a plugin if you want to change it dynamically. FlutterPluginRegistrar has a view property that gives you the Flutter view, then you can get that view's window.
You can do this easily with the window_manager package.
import 'package:window_manager/window_manager.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await windowManager.ensureInitialized();
windowManager.setAlwaysOnTop(true);
runApp(const MyApp());
}

Error: Entrypoint doesn't contain a main function Flutter

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.