DEV Community

Cover image for 7 Hot 🔥 features that will get you started with dart ⏭️
Denyse
Denyse

Posted on • Updated on

7 Hot 🔥 features that will get you started with dart ⏭️

Hello folks, In this series, this is the 3rd chapter of what to know about dart before getting started to flutter have a look :
Before that I strongly recommend the previous chapters to help you get a clear vision of what dart is...

Image description

1. Difference between const, final and var 🤔

  • Final means single-assignment: a final variable or field must have an initializer. Once assigned a value, a final variable's value cannot be changed. final modifies variables. Final value can only be determined at run time. Thus like when using DateTime.now() since the current date and time can only be
    determined at runtime, not at compile time.

  • Const has a meaning that's a bit more complex 😯 and subtle in Dart. const modifies values. You can use it when creating collections, like const [1, 2, 3], and when constructing objects (instead of new) like const Point(2, 3). Here, const means that the object's entire deep state can be determined entirely at compile time and that the object will be frozen and completely immutable.

  • Then for var is completely mutable as its value can be reassigned at any point. final variables can only be assigned once, but by using objects, you can change the value of its fields. const variables are compile-time constants and are fully immutable; nothing about these variables can be changed once
    they've been assigned.

2.Why should we use immutable data types ?

There are two primary benefits of using immutable data types which are:

  • It's faster. When you declare a const value, the compiler has less work to do. It only has to allocate memory for that variable once and doesn't need to worry about reallocating if the variable is reassigned. This may seem like an infinitesimal gain, but as your programs grow, your performance gain grows as well.
  • Immutable data does not have side effects. One of the most common sources of bugs in programming is where value is changed in one place, and it causes an unexpected cascade of changes. If the data cannot change, then there will be no cascade. And in practice, most variables tend to only be assigned once anyway, so why not take advantage of immutability?

3. String buffer in dart

The first interesting thing about strings in dart is that it's okay if you use single quotes or double quotes they both work 😝
It's probably worth mentioning another way to perform concatenation tasks, which is using the StringBufferobject. Consider the following code:

List fruits = ['Strawberry', 'Coconut', 'Orange', 'Mango', 'Apple'];
StringBuffer buffer = StringBuffer();
for (String fruit in fruits) {
 buffer.write(fruit);
 buffer.write(' ');
}
print (buffer.toString()); // prints: Strawberry Coconut Orange Mango Apple
Enter fullscreen mode Exit fullscreen mode

As you can use a StringBuffer to incrementally build a string. This is better than using
string concatenation as it performs better. You add content to a StringBuffer by calling
its write method. Then, once it's been created, you can transform it into a String with the
toString method.

4. Types of parameters in dart
Dart has two types of parameters which are > named and positional parameters.

**Positional optional parameters **are those parameters that are used if you wrap your function's parameter list in square brackets [ ], then those parameters can be omitted without the compiler throwing errors. The question mark after a parameter, such as in String? name, tells the Dart compiler that the parameter itself can be null. Remember we covered this in the previous chapter 😝 so here by they are used like this:

void unnamed([String? name, int? age]) {
 final actualName = name ?? 'Unknown';
 final actualAge = age ?? 0;
 print('$actualName is $actualAge years old.');
}

Enter fullscreen mode Exit fullscreen mode

One more thing to add also supports named optional parameters with curly braces. Then When calling a function with named parameters, you need to specify the
parameter name. You can call the parameters in any order; for
example, named(greeting: 'hello!');. And Dart also supports default values for parameters.

5. Closures

Closures, also known as first-class functions, are an interesting language feature that emerged from lambda calculus in the 1930s. The basic idea is that a function is also a value that can be passed around to other functions as a parameter. These types of functions are called closures, but there is really no difference between a function and a closure. Closures can be saved to variables and used as parameters for other functions. They are
even written inline when consuming a function that expects a closure as a property.
We can optimise the use of closures using typedef keyword have a look:

typedef NumberGetter = int Function();
int powerOfTwo(NumberGetter getter) {
 return getter() * getter();
}
Enter fullscreen mode Exit fullscreen mode

6. Relationships between classes in dart
Classes

7. Grouping data and manipulating data in dart

Now as you are familiar with other languages you know that each language has a way to organize data, you can use one of these collections:

  • Lists are linear collections where the order of the elements is maintained.
  • Maps are a non-linear collection of values that can be accessed by a unique key.
  • Sets are a non-linear collection of unique values where the order is not maintained If Dart is not your first programming language, then this matrix should help you correlate collections to equivalent concepts in other languages:

Collections

One thing that these collections have in common is a subscript syntax. Subscripts are a way to quickly access elements in a collection, and they tend to work identically from language to
language.
like:

numbers[1] = 15;
Enter fullscreen mode Exit fullscreen mode

Horrayyy!!!! ✈️ Thanks fo reading to this far now you're good to go with dart this marks the end of our series about the introduction to dart. Thank you 😉, stay tuned for more articles 🎉
And for further explanation please head to dart.dev.

Top comments (5)

Collapse
 
andrewbaisden profile image
Andrew Baisden

Dart is such a nice language to use.

Collapse
 
dmutoni profile image
Denyse

and interesting

Collapse
 
twizelissa profile image
Elissa Twizeyimana

thanks for sharing

Collapse
 
ekimcem profile image
Ekim Cem Ülger

JS and Dart 👌

Collapse
 
gahamanyi profile image
Gahamanyi-dev

Interesting, thanks for the article