Logo

dev-resources.site

for different kinds of informations.

Null or Nothing? Unmasking the Mystery of Parameters in Dart

Published at
5/19/2024
Categories
flutter
dart
tip
null
Author
rlazom
Categories
4 categories in total
flutter
open
dart
open
tip
open
null
open
Author
6 person written this
rlazom
open
Null or Nothing? Unmasking the Mystery of Parameters in Dart

Sometimes, in the world of development, things get a little confusing. Is a parameter null because it was explicitly passed that way, or simply because it wasn't specified? It's like trying to decipher a riddle: was it a "no" or a "don't know"?

In Dart, the distinction between a null parameter and one that hasn't been specified can be crucial. To prevent our code from turning into a soup of letters, we need a way to unmask this mystery.

Introducing the Marker Technique!

Imagine a magical object called _unspecified. This object holds no information, it merely exists to tell us that a parameter hasn't been defined. With a bit of code magic, we can use this object as a beacon to distinguish between an intentional null and a lack of information.

const Object _unspecified = Object();
Enter fullscreen mode Exit fullscreen mode

Now, let's create a function that looks for this magical object:

bool _isUnspecified(value) => identical(value, _unspecified);
Enter fullscreen mode Exit fullscreen mode

In our code, we can use this function to figure out if a parameter is null by design, or if it was simply not passed.

A Practical Example!

Let's imagine the SpaceState class. In its copyWith function, we want the successMessage and errorMessage parameters to be able to be null, but we also want the possibility for them to simply not be specified, preserving the original value.

class SpaceState {
  final bool isLoading;
  final String? successMessage;
  final String? errorMessage;

  SpaceState copyWith({
    bool? isLoading,
    Object? successMessage = _unspecified,
    Object? errorMessage = _unspecified,
  }) {
    return SpaceState(
      isLoading: isLoading ?? this.isLoading,
      successMessage: _isUnspecified(successMessage) ? this.successMessage : successMessage as String?,
      errorMessage: _isUnspecified(errorMessage) ? this.errorMessage : errorMessage as String?,
    );
  }
}
Enter fullscreen mode Exit fullscreen mode

Eureka!

With this approach, we can avoid errors and keep our code clean and readable. The marker technique allows us to differentiate between "null" and "not specified" with a clarity that brings us closer to achieving perfection in our code.

Remember:

  • The marker technique is a creative and flexible solution, but it can also add a bit of complexity to your code.
  • It's crucial to maintain consistency in using the marker to avoid confusion and errors.

With this technique, your code will speak clearly and help you create robust and easy-to-understand applications!

tip Article's
30 articles in total
Favicon
Ctrl+Alt+Arrow (Right, Left) not working on IntelliJ
Favicon
if locals == globals
Favicon
Version Control Best Practices with Git and GitHub
Favicon
Creating generic types for API (backend) responses
Favicon
Null or Nothing? Unmasking the Mystery of Parameters in Dart
Favicon
List of prompts for successful affiliate marketing
Favicon
My impressions about the book The Clean Coder ๐Ÿงน๐Ÿ“š
Favicon
Why You Should Use GraphQL Playground โฐ
Favicon
Automate WEBP To PNG With A Simple .Bat File
Favicon
In CMS Made Simple, how do you change the theme?
Favicon
A importancia de fazer testes
Favicon
Conditional Styles with CSS :has
Favicon
Do you know that 0.1 + 0.2 is not equal to 0.3?
Favicon
How to save datetime data that is relevant to multiple countries or timeย zones?
Favicon
What I've Learned About Git from Senior Colleagues (Part 1 - git stash)
Favicon
A (somewhat) deep dive into TypeScript constructor intricacies, step-by-step
Favicon
Faster Color picking in Tailwind
Favicon
Evita usar UpperCase o LowerCase C#
Favicon
#DeveloperTipOfTheWeek - Application Security
Favicon
Running out of space on a developer's machine
Favicon
Alert vs confirm in javascript
Favicon
Quick Tip: Counting up to a limit
Favicon
Quick Tip: findFile
Favicon
Time Saving Tip #2 - User Snippets in VSCode
Favicon
Notify Yourself After Completing a Long-Running Bash Process
Favicon
Time Saving Tip #1 - Use Voice Dictation
Favicon
๐Ÿš€ Unveiling the Power of OpenSearch in 202$: A Comprehensive Overview๐Ÿ˜Ž
Favicon
Quick Tip: Checking if a Number is in Range
Favicon
Building a TypeScript Simple Channel (Like Golang)
Favicon
Ubuntu Minimal Install

Featured ones: