Is a constant variable more performant when switching between widgets and if so how do you achieve this at the entry point of app? - flutter

I try to use the same code for both web and android. Where the code differs I switch between widgets based on a global variable.
Is the performance worse when using a non constant / non final variable when switching between widgets? I'm thinking, because the variable is not final or constant and can be changed at any point, Flutter will not be able to optimise the code. Is that true? If inefficient, how do I make my code efficient?
eg.
I have two main files and set my AppType enum in each
[appType.dart]
AppType appType; //can't think of how to make this constant or final
[android_main.dart]
void main() {
appType = AppType.and;
[web_main.dart]
void main() {
appType = AppType.and;
In my widgets I switch where I need a widget specific for the web or android
if(appType == AppType.web)
return MyWidgetWeb();
else
return MyWeigetAnd();

Yes, a constant is more efficient, mainly because of tree-shaking.
Assume that you have the following types:
enum AppType {
mobile,
web,
}
class Mobile {}
class Web {}
Then when you write:
const type = AppType.web;
void main() {
if (type == AppType.web) {
print(Web());
}
else if (type == AppType.mobile) {
print(Mobile());
}
}
Then when compiling the code, the compiler knows that the if block will always be reached, and the else if never will.
As such:
the conditions are removed. When compiled, the code will be:
const type = AppType.web;
void main() {
// no `if` performed
print(Web());
}
Mobile will not be bundled in the executable, so you have a lighter application.
To fully benefit from this behavior, you can use Dart "defines", using int/bool/String.fromEnvironment, which allows you to define constants that behave differently depending on some external build parameters.
The way such constant would look like is:
const isWeb = bool.fromEnvironment('isWeb', defaultValue: false);
Which you can then control using arguments on flutter run and flutter build commands:
flutter build <whatever> --dart-define=isWeb=true

Related

"#define" equivalent in Dart/Flutter?

I wonder if there is a way to #define short_expression long_expression in Dart/Flutter?
For example, instead of typing
MediaQuery.of(context).size.width
// or
Locale.of(context).translate("x")
in every build function, we can
#define MQWidth MediaQuery.of(context).size.width
// or
#define Lt(x) Locale.of(context).translate("x")
then use it in every build function instead?
Macros. Generative programming aka metaprogramming is usually not a thing in interpreted languages. The closest thing to generative programming concept in dart would be static metaprogramming that is actively being chased by. You can currently achieve source code generation in dart via build_runner, which I believe you have seen for example in packages such as json_serializable, and retrofit. But it's still way too far from perfect and requires a lot of work to achieve something even minuscule. And it only fits into certain scenarios and the example you gave is sadly not one of them.
So, the answer is no. You can't do that, at least as of now.
But instead why don't you just separate that into functions if they are really overused all over the place. I know you still have to pass in the context everywhere which is kinda cluttering compared to what you have expected. But it'll still make the code much shorter, if that's your goal.
Hope this answers your question. Cheers!
An example of how this can be done.
By the way, this is the simplest example. Without using of the macro arguments (AST nodes).
Input: bin/main.dart
#pragma('meta_expression:build')
library my_cool_library;
#pragma('meta_expression:import')
import 'package:test_meta_expression/my_cool_macros.dart';
void main(List<String> args) {
final a = mqWidth();
final b = lt('Hello!');
print(a);
print(b);
}
Output: bin/main.impl.dart
// GENERATED CODE - DO NOT MODIFY BY HAND
// **************************************************************************
// MetaExpressionLibraryGenerator
// **************************************************************************
library my_cool_library;
void main(List<String> args) {
//
final a = MediaQuery.of(context).size.width;
final b = Locale.of(context).translate('Hello!');
print(a);
print(b);
}
Macro: package:test_meta_expression/my_cool_macros.dart
import 'package:meta_expression_annotation/meta_expression_annotation.dart';
#MetaExpression(mqWidthImpl)
external int mqWidth();
String mqWidthImpl(MetaContext context) => '''
MediaQuery.of(context).size.width
''';
#MetaExpression(ltImpl)
external String lt(String x);
String ltImpl(MetaContext context) => '''
Locale.of(context).translate(x)
''';
File: pubspec.yaml
dependencies:
meta_expression_annotation: 0.2.1
dev_dependencies:
build_runner: any
meta_expression: 0.2.1
Build command:
dart run build_runner build

Flutter testing: static method I need to mock inside code

I want to test a method that is responsible for a button tap (let's call it onButtonTap()), one of the first methods is a call to static method from utils file, that returns true of false, depending on set android/ios permissions (or allows user to change permissions by showing dialog that can open application settings). Let's call it checkOrRequestPermissions(). This makes everything behind that code untestable, as I don't know how to test it - I can't mock this class because:
It's not injected anywhere - it's inside utils file
It's static
So for better visualization lets go like this:
Code from file I want to test:
Future<void> onButtonTap(BuildContext context) async {
bool isGranted = await PermissionsUtil.checkOrRequestPermissions([some_args]);
// CODE_A - some code I want to test
}
Code inside PermissionsUtil:
class PermissionsUtil{
static Future<bool> checkOrRequestPermissions([some_args]){
// code for permissions
}
}
So my questions are:
Is there any way I could mock checkOrRequestPermissions() to simply return given value?
How could I make this code testable?

Flutter Riverpod design pattern (inhibit garbage collection)

I've written a Swift/IOS package to externalize and standardize all of my Social/Federated/Firebase authentication boilerplate (both SDK's and UI). I've taken it upon myself to port this to Flutter as a learning exercise ... but to also allow custom UI to be passed-in via config.
Since I'm new to Flutter & Riverpod, I'm afraid I'm making some serious mistake & want to get feedback from you experts before I go too deep.
The package is called "Social Login Helper" or SLH, and this is the public API I desire:
runApp(
slh.authStateBuilder(
builder: (authStatus) {
switch (authStatus.stage) {
case SlhResultStage.initializing:
return SplashScreen();
case SlhResultStage.unauthenticated:
// using Riverpod and Nav 2.0
return slh.authFlowUi;
case SlhResultStage.authenticated:
return ExampleApp(appKey, authStatus, slh.logoutCallback);
case SlhResultStage.wantsAnnonOnlyFeatures:
return ExampleApp(appKey, null, slh.startAuthCallback);
case SlhResultStage.excessiveFailures: // restart the app
return TotalFailure();
}
},
),
);
As you can see from the above, the State/Stream builder at root must never be garbage collected or purged. I'm unclear if and when Riverpod will dispose my provider, or if Dart itself will collect objects that must remain immortal. I'm also unsure whether to use a StreamProvider or a State provider??
As you can see below, I've created an intentional memory-leak (deadlock) to guard me. I'm sure it's an anti-pattern, but being novice, I'm not sure how else to guarantee immortality.
All guidance and explicit feedback would be most welcome.
class LivingAuthState extends StateNotifier<SlhResultStage> {
// create deadly embrace to prevent this from ever being collected
_Unpurgeable _up;
LivingAuthState() : super(SlhResultStage.initializing) {
//
final StreamProvider<SlhResultStage> rssp =
StreamProvider<SlhResultStage>((ref) {
return this.stream.asBroadcastStream();
});
_up = _Unpurgeable(this, rssp);
// how do I keep rssp from ever being collected??
}
StreamProvider<SlhResultStage> get authStatusStream => _up.rssp;
void logout() {
this.state = SlhResultStage.unauthenticated;
}
void restartLogin() {
this.state = SlhResultStage.unauthenticated;
}
}
class _Unpurgeable {
final LivingAuthState _aliveState;
final StreamProvider<SlhResultStage> rssp;
_Unpurgeable(this._aliveState, this.rssp);
}
One improvement I'd like to see in the Riverpod documentation is clarity on HOW LONG a provider will live, WITHOUT an active listener, before it will self-dispose / garbage-collect.
Ah, it looks like I can subclass AlwaysAliveProviderBase() to achieve the same goal ... I'll experiment with this.
Move your provider final to the top level. Riverpod providers are top-level final variables.
Also remember to wrap your app in the riverpod provider.

How to create global variables using Provider Package in Flutter?

My Flutter app needs a global variable that is not displayed(so no UI changes) but it needs to run a function everytime it is changed. I've been looking through tutorials etc. but they all seem to be for much more complicated uses than what I need and I'd prefer to use the simplest approach that is still considered "good practice".
Roughly what I am trying to do:
//inside main.dart
int anInteger = 0;
int changeInteger (int i) = {
anInteger = i;
callThisFunction();
}
//inside another file
changeInteger(9);
You can make a new Class in a new file to store the global variable and its related methods. Whenever you want to use this variable, you need to import this file. The global variable and its related methods need to be static. Pay attention to the callThisFunction that you mentioned in your question, it needs to be static as well (since it would be called in a static context). e.g.
file: globals.dart
class Globals {
static var anInteger = 0;
static printInteger() {
print(anInteger);
}
static changeInteger(int a) {
anInteger = a;
printInteger(); // this can be replaced with any static method
}
}
file: main.dart
import 'globals.dart';
...
FlatButton(
onPressed: () {
Globals.changeInteger(9);
},
...

How can I check if a Flutter application is running in debug?

I'm looking for a way to execute code in Flutter when the app is in Debug mode. Is that possible in Flutter? I can't seem to find it anywhere in the documentation.
Something like this
If(app.inDebugMode) {
print("Print only in debug mode");
}
How can I check if the Flutter application is running in debug or release mode?
In later versions, you can use kDebugMode:
if (kDebugMode)
doSomething();
While asserts can technically be used to manually create an "is debug mode" variable, you should avoid that.
Instead, use the constant kReleaseMode from package:flutter/foundation.dart
The difference is all about tree shaking.
Tree shaking (aka the compiler removing unused code) depends on variables being constants.
The issue is, with asserts our isInReleaseMode boolean is not a constant. So when shipping our app, both the dev and release code are included.
On the other hand, kReleaseMode is a constant. Therefore the compiler is correctly able to remove unused code, and we can safely do:
if (kReleaseMode) {
} else {
// Will be tree-shaked on release builds.
}
Here is a simple solution to this:
import 'package:flutter/foundation.dart';
Then you can use kReleaseMode like
if(kReleaseMode){ // Is Release Mode??
print('release mode');
} else {
print('debug mode');
}
Please use Remi's answer with kReleaseMode and kDebugMode or Dart compilation won't be able to tree-shake your code.
This little snippet should do what you need:
bool get isInDebugMode {
bool inDebugMode = false;
assert(inDebugMode = true);
return inDebugMode;
}
If not, you can configure your IDE to launch a different main.dart in debug mode where you can set a Boolean.
kDebugMode
You can now use the kDebugMode constant.
if (kDebugMode) {
// Code here will only be included in debug mode.
// As kDebugMode is a constant, the tree shaker
// will remove the code entirely from compiled code.
} else {
}
This is preferable over !kReleaseMode as it also checks for profile mode, i.e., kDebugMode means not in release mode and not in profile mode.
kReleaseMode
If you just want to check for release mode and not for profile mode, you can use kReleaseMode instead:
if (kReleaseMode) {
// Code here will only be run in release mode.
// As kReleaseMode is a constant, the tree shaker
// will remove the code entirely from other builds.
} else {
}
kProfileMode
If you just want to check for profile mode and not for release mode, you can use kProfileMode instead:
if (kProfileMode) {
// Code here will only be run in release mode.
// As kProfileMode is a constant, the tree shaker
// will remove the code entirely from other builds.
} else {
}
While this works, using constants kReleaseMode or kDebugMode is preferable. See Rémi's answer below for a full explanation, which should probably be the accepted question.
The easiest way is to use assert as it only runs in debug mode.
Here's an example from Flutter's Navigator source code:
assert(() {
if (navigator == null && !nullOk) {
throw new FlutterError(
'Navigator operation requested with a context that does not include a Navigator.\n'
'The context used to push or pop routes from the Navigator must be that of a '
'widget that is a descendant of a Navigator widget.'
);
}
return true;
}());
Note in particular the () at the end of the call - assert can only operate on a Boolean, so just passing in a function doesn't work.
Not to be picky, but the foundation package includes a kDebugMode constant.
So:
import 'package:flutter/foundation.dart' as Foundation;
if(Foundation.kDebugMode) {
print("App in debug mode");
}
I believe the latest way to do this is:
const bool prod = const bool.fromEnvironment('dart.vm.product');
src
Just import this
import 'package:flutter/foundation.dart'
String bulid = kReleaseMode ? "Release" : "";
or
String bulid = kDebugMode ? "Debug" : "";
or
String bulid = kProfileMode ? "Profile" : "";
Or try this
if (kDebugMode) {
print("Debug");
} else if (kReleaseMode) {
print("Release");
} else if (kProfileMode) {
print("Profile");
}
Make a file named constants.dart. Add these variables in it:
const bool kReleaseMode = bool.fromEnvironment('dart.vm.product');
const bool kProfileMode = bool.fromEnvironment('dart.vm.profile');
const bool kDebugMode = !kReleaseMode && !kProfileMode;
printk(String string) {
if (kDebugMode) {
// ignore: avoid_print
print(string);
}
}
Then import this constant file in any other file and use it like this:
import 'package:package_name/constants.dart';
if(kDebugMode){
//Debug code
}else{
//Non-Debug code
}
printk("Debug Log");
I've created this useful class, based on other answers and inspired on Android usage.
If anything changes on "Foundation" package, it would not be necessary to change the entire application, it would be necessary to change only this class.
import 'package:flutter/foundation.dart' as Foundation;
abstract class Build {
static const bool isDebugMode = Foundation.kDebugMode;
static const bool isReleaseMode = Foundation.kReleaseMode;
static const bool isWeb = Foundation.kIsWeb;
static const bool isProfileMode = Foundation.kProfileMode;
}
Extracted from Dart Documentation:
When exactly do assertions work? That depends on the tools and
framework you’re using:
Flutter enables assertions in debug mode.
Development-only tools such as dartdevc typically enable assertions by default.
Some tools, such as dart and dart2js, support assertions through a command-line flag: --enable-asserts.
In production code, assertions are ignored, and the arguments to
assert aren’t evaluated.