0% found this document useful (0 votes)
22 views6 pages

Dart Notes

Uploaded by

justcheck2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views6 pages

Dart Notes

Uploaded by

justcheck2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

1.

Different datatypes available in Dart

int a = 10 // For integers


double a = 2.5 // For floating values
num a = 4 or 10.2 // If both integers and floating points are required
String abc = ‘Never odd or even’ // Simply string
bool isLocationEnabled = false // Either true or false
List price = [200, 120.25, ‘Hello’] // List with different types of data
Map empList = {01 : “ABC”, 02 : “XYZ”) // Map with key-value pairs
dynamic a = 10 // Dynamic datatype, possible to modify a to “hello”
var a = 10 // Detects and fixes the datatype of ‘a’ for future

List books = [‘Harry Potter’, ‘Twilight’] // books.runtimeType returns datatype as dynamic


List <String> booksNew = [……………..] // booksNew.runtimeType returns datatype as string
List _books = [‘Harry Potter’] // Private variable, can not be accessed outside ‘file’
Map <int, String> List = {01 : “ABC”) // Can contain only int-string pairs

2. Operations on datatypes

print(a.isEven) // Returns boolean value, used for integers


print(a.round()) // Returns rounded off value, used for double
print(abc.contains(‘odd’)) // Returns boolean value, used for strings
print(abc.startsWith(‘Never’)) // Returns boolean value, used for strings
print(abc.isEmpty) // Returns boolean value, used for strings
print(abc.toUpperCase()) // Converts all leters to Uppercase
print(books.runtimeType) // Returns the datatype of books
books.add(1234) // Adds the number 1234 to books list
booksNew.add(1234) // Error. Can only add string type data
print(books.length) // Returns length of books list
print(abc.length) // Returns length of string
books.clear() // Deletes all the elements of the list
empList.addAll({03 : “KLM”}) // Adds all the specified key-value pairs to map

For more operations, visit api.dart.dev and search for particular datatype class in search box.

3. How to print a variable’s value directly in the form of a string?

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

6. How do you define a function with some parameters as optional?

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

abc("Apples", 10); // Output: I have 10 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

abc(b: 10, a: "Apples"); // Output: I have 10 Apples

8. How do you define a function with only one statement inside?

int area (int length, int breadth) => length*breadth

void main ()
{
print(area(4,5)); // Output: 20
}

9. Is it mandatory to specify datatypes in function parameters?

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.

void abc (a,b) // ab are dynamic, ab are mandatory, positional


void abc (a, [int? b]) // a is dynamic, a is mandatory, positional
void abc ([a, int? b]) // a is dynamic, all are optional, positional
void abc ({a,int? b}) // a is dynamic, all are optional, non-positional
10. What is a generative constructor? How do you declare it?

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;

Person (this._name, this._age);


}
However if we wish to access or modify a private variable from another file, then we will need the
intermediators – getter and setter functions.

void main()
{
Person abc = Person("Mayank",21);
print(abc.currentAge);
abc.newAge = 23;
print(abc.currentAge);
}

class Person
{
String? _name;
int? _age;

int? get age => _age;

set age (int x)


{
_age = x;
}

Person (this._name, this._age);


}

12. How do you make a variable non-changeable forever?

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.

final int aadhaar; // Immutable, mandatory initialisation


late final int aadhaar; // Immutable but can be initialised later

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.

13. Can we make multiple constructors for a single class?

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.

Person (this._name, this._age);


Person.withAadhaar (this._name, this._age, this.aadhaar);

Person abc = Person("Mayank",21);


Person abc = Person.withAadhaar("Mayank",21, 123456789);

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy