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!

null Article's
30 articles in total
Favicon
How Imburse Payments Ships High-Quality APIs Faster
Favicon
Need to Verify Your JSON Schema? Here's a Few Ways to Do It!
Favicon
Code Smell 260 - Crowdstrike NULL
Favicon
Be careful when using NULL in PostgreSQL
Favicon
The most painful reason NULLs are evil
Favicon
Null or Nothing? Unmasking the Mystery of Parameters in Dart
Favicon
La Solución del Billón de Dólares
Favicon
11 Lessons to learn when using NULLs in PostgreSQL®
Favicon
NULLs Are Not The Same – A Guide
Favicon
the (not so big) Bang!
Favicon
Rust's Option type... in Python
Favicon
Understanding Nullable Reference Types in C#
Favicon
Working with NULL in Databases. Turn Your Frustration Into Delight
Favicon
TypeScript: The many types of nothing
Favicon
ERROR: null" or "null pointer exception while invoking FlowService - Storage get operation
Favicon
ServiceNow: 1 thing for safer GlideRecord scripts
Favicon
How NullPointerException can be avoided in Java
Favicon
Consider these facts when dealing with NULL in RDBMS
Favicon
Unhandled Exception: type 'Null' is not a subtype of type 'int' in type cast error when trying to call function with no int
Favicon
When <nil> is not <nil>
Favicon
Absence of null in Solidity
Favicon
How to Check for Null in Javascript
Favicon
Javascript Tagalog - Null
Favicon
Kotlin 基礎 Part 1 -- !! や ?: と ?.let で Nullable な値を処理する
Favicon
Remove null check, use the Optional
Favicon
Handling null: optional and nullable types
Favicon
More JS Concepts
Favicon
Valores null e undefined no JavaScript
Favicon
Javascript default parameter for null and undefined
Favicon
How I Learned to Stop Worrying and Love NULL in SQL

Featured ones: