Use the final
keyword:
In Dart, variables that are initialized and cannot be changed are declared using the final
keyword. Your code will become more predictable and help to prevent bugs.
Here’s an example:
final String name = 'Snehal Singh';
name = 'Snehal Singh'; // Error: The final variable 'name' can't be assigned a value.
In this example, we declare a variable name
and assign it the value 'Snehal Singh'. Since name
is declared as final
, we cannot reassign it to a new value.
Use named parameters:
Using named parameters in function calls is supported in Dart, which can improve the readability and comprehension of your code.
You have more control over the order in which arguments are passed to functions when using named parameters.
Here’s an example:
void greet({String name, String message}) {
print('$name says $message');
}
greet(name: 'Snehal Singh', message: 'Hello, world!'); // Snehal Singh says Hello, world!
In this example, we define a function greet
that takes two named parameters, name
and message
.
We can then call the function and pass in the arguments in any order we
like, as long as we specify the names of the parameters.
Use null-aware operators:
Dart offers a number of null-aware operators that can make your code shorter and more productive.
For instance, you can safely access the properties of an object that might be null by using the ?.
operator.
Here's an example:
class Person {
final String name;
final int age;
Person({this.name, this.age});
}
final person = Person(name: 'Snehal Singh', age: null);
final name = person?.name ?? 'Unknown';
final age = person?.age ?? -1;
print(name); // Snehal Singh
print(age); // -1
In this example, we define a class Person
that has two properties, name
and age
. We create an instance of Person
and assign null
to the age
property. We then use the null-aware operator ?.
to safely access the name
and age
properties, and the null-coalescing operator ??
to provide default values in case they are null
.
Use extension methods:
Using extension methods, Dart lets you add new methods to classes that already have them.
This can be helpful if you want to expand a class’s functionality without changing its source code.
Here’s an example:
extension StringExtension on String {
bool get isPalindrome {
final reversed = this.split('').reversed.join('');
return this == reversed;
}
}
print('racecar'.isPalindrome); // true
print('hello'.isPalindrome); // false
In this example, we define an extension method isPalindrome
on the String
class. The method checks if the string is a palindrome and returns true
or false
.
Use async/await:
Asynchronous programming is supported in Dart using the async
and await
keywords.
This can assist you in writing more responsive code that doesn’t obstruct the UI thread.
Here's an example:
Future<void> main() async {
print('Fetching data...');
final data = await fetchSomeData();
print('Data received: $data');
}
Future<String> fetchSomeData() async {
await Future.delayed(Duration(seconds: 2)); // Simulate network delay
return 'Hello
In this example, we define a function main
that is marked as async
. Inside main
, we call a function fetchSomeData
using the await
keyword. This tells Dart to wait for the fetchSomeData
function to complete before continuing the execution of the rest of the main
function. fetchSomeData
simulates a network delay using the Future.delayed
function and returns a string.
When we run the main
function, we first print a message to the console indicating that we are fetching data. We then call fetchSomeData
and wait for it to complete before printing the received data to the console.
Use the cascade operator:
The cascade operator (..
) in Dart enables you to chain together several method calls on the same object.
Your code may become clearer and easier to read as a result.
Here's an example:
class Person {
String name;
int age;
void sayHello() {
print('Hello, my name is $name and I am $age years old');
}
}
final person = Person()
..name = 'Snehal Singh'
..age = 30
..sayHello(); // Hello, my name is Snehal Singh and I am 30 years old
In this example, we define a class Person
that has two properties, name
and age
, and a method sayHello
. We create an instance of Person
and use the cascade operator ..
to set the name
and age
properties and call the sayHello
method in a single chain of method calls.
Source:
https://medium.com/dhiwise/modern-flutter-6-tips-and-tricks-for-beginner-developers-e4094de061bb
0 comments:
Post a Comment