Flutterexperts

Empowering Vision with FlutterExperts' Expertise
Important Dart Concepts In Flutter

In this blog, we shall learn about various Null-aware operators, where to use const keyword, async, and await. Dart is a powerful language that provides us, various special operators, keyword. Operators are the special characters that are used to perform various operations on the operants. There are four null-aware operators that are used to handle the null values. const keyword is used to reduce the rebuild of const object every time.


Null- aware Operators

Operators that deals with values that might be null during runtime are called Null-aware Operators.

  • ?. operator

This operator is used when you call a method on an object if that object is not null.

Eg. If we have a String value , we want to return null if it is null and if not then value.toLowerCase() , then we can use value?.toLowerCase() .

It is equivalent to value ==null?null:value.toLowerCase() .

  • ??= operators

It is called a null-aware assignment.

void main() {
String string;
//if (string == null) string = 'Hi!';
string ??= 'Hi!';
print(string);
}
  • ?? operators : It is a null operator.
void main() {
String string = "Hi!";
print(string ?? string??2);
}

Output: Hi!

void main() {
String string;
print(string ?? string??2);
}

Output: 2

In the first program, the output is Hi! while in the second program the output is 2 . As we can see in the first program the value of the string is not null that is why it has returned the left part of the statement string ?? string??2 . While in the second program string is null hence it has returned the right part of the statement i.e. 2

  • … operator

This operator is amazing. It adds a list that is not null with the other list of the same type.

void main() {
List<int> list = [1,2,3];
List<int> list1 = [4,5,6];
List<int> finalList = [0,...list,...list1];
print(finalList);
}

Output:[0, 1, 2, 3, 4, 5, 6] .

Use of const keyword

const keyword in dart is highly effective to use as

  • It increases the performance of our app.
  • Reduce CPU load.
  • Allocate only one memory space.

Using const keyword while construction an objectconst Text("Hi!"), while creating a collection const [3,4,2,5] .

If we use const like this, it will work absolutely fine.

void main() {
const int value = 2;
const int _value = 3;
const int result = value + _value;
print(result);
}

But if we use const DateTime.now() , this will not work as it changes and it needs to be rebuilt every time the user opens the app. We will get the following error.

The constructor being called isn’t a const constructor. Try removing ‘const’ from the constructor invocation.

  • : A const constructor can’t be assigned for a class.
  • : If you repeat the const object in the same class it will reuse the same object created for the first time.
  • : The const objects are frozen and completely Immutable.
  • : Const variables are implicitly final i.e. Compile-time constant.

What is async and await ?

These two keywords are highly useful for updating our UI while processing our data. Example if we use authentication in our app then while signing up, signing in, logout there is a small-time delay while performing these operation, so it would be nice if we can display a loading spinner while these operation gets executed.

For this purpose, we can use async and await keyword in our authentication code and we can build our logic for displaying our logic.

While creating a function for authentication make that function asynchronous by using the async keywordauth()async{} . await is used to show up the Circular Progress Indicator until the Code Execution is Completed.

auth()async{
//set the loading spinner state var signIn = await authentication();
//reset the state of loading spinner
}

Thanks for reading this article ❤

If I got something wrong? Let me know in the comments. I would love to improve.

Clap 👏 If this article helps you.

If we got something wrong? Let me know in the comments. we would love to improve.


FlutterDevs team of Flutter developers to build high-quality and functionally-rich apps. Hire flutter developer for your cross-platform Flutter mobile app project on an hourly or full-time basis as per your requirement! You can connect with us on Facebook, GitHub, Twitter, and LinkedIn for any flutter related queries.

We welcome feedback and hope that you share what you’re working on using #FlutterDevs. We truly enjoy seeing how you use Flutter to build beautiful, interactive web experiences!.

Leave comment

Your email address will not be published. Required fields are marked with *.