Flutter only import library in debug mode - flutter

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.

Related

How do I use local auth and bar code scanner in the same flutter project?

I building a flutter app that scans bar codes and also uses biometric auth in it. The problem is for package local_auth
MainActivity.kt should be like
import io.flutter.embedding.android.FlutterFragmentActivity;
public class MainActivity extends FlutterFragmentActivity {
// ...
}
but for scaning bar codes MainActivity.kt should be like
import io.flutter.embedding.android.FlutterActivity
class MainActivity: FlutterActivity() {
}
else it don't work. How do I use them both in the same project?
Currently you have to replace FlutterActivity with FlutterFragmentActivity yourself in FlutterBarcodeScannerPlugin.java (like this).
So you would want to use a fork of the flutter_barcode_scanner plugin where FlutterFragmentActivity is used or you use some other barcode scanner plugin.

How do I use multiple main activities in flutter?

I am building a flutter app which has these two plugins 1) local_auth 2) flutter_bar_code and they both use different main activities , for local auth the MainActivity.kt is like
import io.flutter.embedding.android.FlutterFragmentActivity
class MainActivity: FlutterFragmentActivity() {
}
and for flutter bar code scanner the MainActivity.tk is like
import io.flutter.embedding.android.FlutterActivity
class MainActivity: FlutterActivity() {
}
Flutter has a single MainActivity file. if you want to add more screen then please read doc and blogs here is one for you: Add Multiple screen

Flutter run Dart function in the background

I have a dart function someFunction which takes a couple of minutes to run (it has a good amount of network requests it awaits on). Currently when I run the function and move my app to the background it seems that the requests that were queued up before leaving the app are executed, but then the function stops, and also doesn't resume when I return to the app. What I would like to do is to have the function run in the background until completion, even if the app is not in the foreground, and also send a notification once the function finishes if the app is not in the foreground.
I've looked at some solutions like the background-fetch plugin and isolates, but unless im missing something they seem to be only for native code, or periodical background tasks. What i'm looking for is a task that runs once no matter what and returns back to the main thread with a result it can react to
The non Dart dependencies of the function incase this is relevant:
import 'package:http/http.dart' as http;
import 'package:shared_preferences/shared_preferences.dart';
import 'package:firebase_crashlytics/firebase_crashlytics.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
have you try this package, workmanager with WidgetsBindingObserver (App lifecycle)?
#override
void didChangeAppLifecycleState(AppLifecycleState state) {
//Do whatever you want in background
if (state == AppLifecycleState.paused ||
state == AppLifecycleState.detached) {
}
}

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

debugPaintSizeEnabled is not working in 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.