Reputation: 53
i work with flutter framework this part of the code use a operation "?." but idont understand
if (state is WeatherLoaded) {
final weather = state.weather;
final themeBloc = BlocProvider.of<ThemeBloc>(context);
themeBloc.dispatch(WeatherChanged(condition: weather.condition));
_refreshCompleter?.complete();
_refreshCompleter = Completer();
all code this link
Upvotes: 3
Views: 3285
Reputation: 6962
The best way to demonstrate this is a simple example.
I have an object SomeObject
with one method username
.
I have made 2 instances of it:
aeonObject
which is not null
someOtherObject
which is null
class SomeObject {
String username() => "aeon";
}
void main() {
final aeonObject = SomeObject();
print(aeonObject.username());
SomeObject someOtherObject;
print(someOtherObject.username());
}
If I execute this snippet you'll see the following output.
The program will crash because we tried to execute a method on a null
reference.
dart lib/main.dart lib/main.dart: Warning: Interpreting this as package URI, 'package:sample/main.dart'.
aeon
Unhandled exception: NoSuchMethodError: The method 'username' was called on null.
Receiver: null Tried calling: username()
However if I call the print
statement with the ?.
aka Conditional member access operator
.
print(someOtherObject?.username());
We instead get.
null
Upvotes: 5
Reputation: 10963
Check this link: Language tour
?.
Conditional member access
Like ., but the leftmost operand can be null; example: foo?.bar selects property bar from expression foo unless foo is null (in which case the value of foo?.bar is null)
Upvotes: 1