Dart: Tips Of The Day
906 subscribers
32 photos
8 links
Author: @plugfox

Chats:
@en_dart
@ru_dart
Download Telegram
Since Flutter 3.7, you can store all your API keys inside a JSON file and pass it to a new --dart-define-from-file flag from the command line.

E.g. --dart-define-from-file=keys.json

#TipOfTheDay #Dart #Flutter #PlugFox
18
Tip of the Day: 🚀 To improve performance in your Flutter apps, use the ListView.builder instead of simply using ListView when dealing with long lists. It only creates items that are visible on the screen and recycles them when they scroll out of view, saving memory and reducing lagging! 🌟


ListView.builder(
itemCount: itemCount,
itemBuilder: (context, index) {
return YourListItemWidget(index);
},
);


Happy coding! 🔥
#TipOfTheDay #Dart #Flutter #Performance #ListViewBuilder #ChatGPT #n8n
8
🚀 Tip of the Day: To avoid potential errors when handling dialog and bottom sheet navigations in your application, consider the following advice:

When displaying a dialog or a bottom sheet, it is shown by default from the ROOT navigator:

showDialog(...);
showModalBottomSheet(...);


However, when calling pop(), it is called from the NEAREST navigator by default:

Navigator.of(context).pop(...);
Navigator.pop(context, ...);


This discrepancy leads to issues where a modal route is added to one navigator and pop is called from another. To ensure consistency and avoid errors, use:

Navigator.of(context, rootNavigator: true).pop(...);


Alternatively, create a helper function to manage the top modal route effectively:

void popDialog(BuildContext context, [Object? result]) {
final state = Navigator.maybeOf(context, rootNavigator: true);
if (state == null || !state.mounted) return;
state.maybePop(result);
}

void popDialogs(BuildContext context) {
final state = Navigator.maybeOf(context, rootNavigator: true);
if (state == null || !state.mounted) return;
state.popUntil((route) => route is! RawDialogRoute<Object?> && route is! ModalBottomSheetRoute<Object?>);
}


Following this approach ensures that pop calls are correctly aligned with the appropriate navigator, preventing wrong and unexpected behavior.

#TipOfTheDay #Dart #Flutter #navigator #pop #plugfox
29
🎯 Tip of the Day: Using Extensions in Dart

Extensions in Dart enhance existing types with new methods, making your code cleaner and more readable. Here's a quick look at two useful extensions: let and ifNull.

Example 1: Let Extension
The let extension wraps an object in a function, allowing you to perform operations and return the result.


extension LetX<T extends Object?> on T {
R let<R extends Object?>(R Function(T it) callback) => callback(this);
}

void main() {
print(14.let((it) => it * 3)); // Outputs 42
}


Example 2: IfNull Extension
The ifNull extension provides a default value if the object is null, ensuring safe handling of nullable types.


extension IfNullX<T extends Object> on T? {
T ifNull(T Function() callback) => this ?? callback();
}

void main() {
int? value;
print(value.ifNull(() => 14) * 3); // Outputs 42
}


#TipOfTheDay #Dart #Flutter #extension #let #ifNull #plugfox
22