Dart Notes
Dart Notes
2. Operations on datatypes
For more operations, visit api.dart.dev and search for particular datatype class in search box.
int sum = 2
print(‘ Answer is $sum’) // Output: ‘Answer is 2’
4. How will you represent a whole object as a string? (Used for debugging and logging)
class Person
{
String name;
int age;
Person(this.name, this.age);
@override
String toString()
{
return 'Person(name: $name, age: $age)';
}
}
void main()
{
var person = Person('Alice', 30);
print(person); // Output: Person(name: Alice, age: 30)
}
5. How would you declare a variable as nullable? (By default, all of them are non-nullable)
List <String?> ………. // Null items can be present along with String items
Map < int, String>?........ // Null Key-value pairs can be present
Map < int, String?>........ // Key can be present with null value
Note : All optional parameters must have the flexibility to hold NULL values in case they are not being passed
during the function call. Therefore it is necessary to make these optional arguments as nullable. Remember
that while declaring a function, optional parameters must be placed to the rightmost side of the round
braces.
void abc (String a, [int? b]) // We can write [int b=100] to override null and set a default value
{
print("I have $b $a");
}
void main ()
{
abc("Apples"); // Output: I have null Apples
}
7. How do you define a function with non-positional paramenters?
Note : This will be useful if we have some arguments to pass but we do not wish to remember the order in
which they need to be passed. Also, using this method makes all the parameters as optional as well.
Therefore we need to add another keyword ‘required’ to tell compiler about non-optional parameters. Non-
positional parameters are also called named parameters because you have to use the parameter names
while object creation. Remember that named parameters are purposefully created to be called outside the
class (and also outside the file), making it easy for the coder to pass arguments using their names. This
means that you can’t use any private variable as a named variable because they are meant not to be called
outside the file. And using a private variable as a named parameter will be contradictory.
void abc ({required String a, int? b}) // Note the curly braces
{
print("I have $b $a");
}
void main ()
{
abc(a: "Apples"); // Output: I have null Apples
void main ()
{
print(area(4,5)); // Output: 20
}
If we do not specify the datatype for any variable in a function parameter, the compiler automatically assigns
it as “dynamic” and thus it will automatically detect the datatype based on the arguments passed. Note that
dynamic datatypes are nullable by default which means they can accept null as a value.
Generative constructor is just an another name for parameterised constructors used in C++. While creating
an instance or object of a class, you pass some values in the braces. This initialises the newly created object’s
variables.
Method-1
class People
{
String? name; // nullable if we do not initialise at the time of creation
int? age; // similar to writing “int age = null”
Person (String name, int age) //optional to specify datatypes but recommended
{
this.name = name;
this.age = age;
}
}
Method-2
class People
{
String? name;
int? age;
Person (this.name, this.age)
}
11. How do you access a private variable outside its own class?
Remember that we are allowed to access any private variable outside its class in Dart, if the access request is
made from the same file as the file containing the class declaration. However, if your class code resides in a
different dart file than the dart file requesting variable access, then we need to use getters and setters.
For example, below code is allowed because class declaration and access requests are being made in the
same file.
void main()
{
Person abc = Person("Mayank",21);
abc._name = "Hello";
print(abc._name);
print(abc._age);
}
class Person
{
String? _name;
int? _age;
void main()
{
Person abc = Person("Mayank",21);
print(abc.currentAge);
abc.newAge = 23;
print(abc.currentAge);
}
class Person
{
String? _name;
int? _age;
We often need to make certain variables non-changeable if we know that they will stay same forever and
should not be affected in case of any attacks or mishappenings to our database. For example, Aadhar card
number is generated only once per person.
Remember that all final variables must be initialised. In the case above, we have used constructor to initialise
the final variable at the time of object creation itself. If we have a final variable whose value is not readily
available with us, then we use late keyword.
The constructor we have been using by now is called a generative constructor, i.e. they generate a new
instance of the class. We can have multiple generative constructors for a single class but all of them should
have different names. This is useful if we wish to create a constructor which takes in different parameters
than already decalred constructors.