If you want a variable of type String to accept any string or the value null , give the variable a nullable type by adding a question mark ( ? ) after the type name. For example, a variable of type String? can contain a string, or it can be null.
The JavaScript double question mark is also known as the nullish coalescing operator. It's an operator that simply returns the right-side expression when the left side expression is either null or undefined .
Equality. (A==B) is False. != Not equal. (A!=B) is True.
This is with null safety, the question mark means that this String? can possibly be null and flutter will allow you to assign null to it. String can never be null and you'll get an error before compiling. If you define a variable String? name , and you want to use it later in a Text widget, you'll get an error.
The ??
double question mark operator means "if null". Take the following expression, for example.
String a = b ?? 'hello';
This means a
equals b
, but if b
is null then a
equals 'hello'
.
Another related operator is ??=
. For example:
b ??= 'hello';
This means if b
is null then set it equal to hello
. Otherwise, don't change it.
Reference
Terms
The Dart 1.12 release news collectively referred to the following as null-aware operators:
??
-- if null operator??=
-- null-aware assignmentx?.p
-- null-aware accessx?.m()
-- null-aware method invocationDart offers some handy operators for dealing with values that might be null. One is the ??= assignment operator, which assigns a value to a variable only if that variable is currently null:
int a; // The initial value of a is null.
a ??= 3;
print(a); // <-- Prints 3.
a ??= 5;
print(a); // <-- Still prints 3.
Another null-aware operator is ??, which returns the expression on its left unless that expression’s value is null, in which case it evaluates and returns the expression on its right:
print(1 ?? 3); // <-- Prints 1.
print(null ?? 12); // <-- Prints 12.
This is especially useful in the copyWith method which is used in flutter very often to override. Here is an example below:
import './color.dart';
import './colors.dart';
class CoreState {
final int counter;
final Color backgroundColor;
const CoreState({
this.counter = 0,
this.backgroundColor = Colors.white,
});
CoreState copyWith({
int? counter,
Color? backgroundColor,
}) =>
CoreState(
counter: counter ?? this.counter,
backgroundColor: backgroundColor ?? this.backgroundColor,
);
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is CoreState &&
runtimeType == other.runtimeType &&
counter == other.counter &&
backgroundColor == other.backgroundColor;
@override
int get hashCode => counter.hashCode ^ backgroundColor.hashCode;
@override
String toString() {
return "counter: $counter\n"
"color:$backgroundColor";
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With