Listado de la etiqueta: Dart


Decision-making expressions are those which let programmers choose which statement to execute under different circumstances. Conditional statements are used in different programming languages to inform the computer on what factors to make when certain conditions are met. These decisions are taken only if the already stated conditions are true or false: it depends on the functions in the mind of the programmer. The if statement, if-else statement, and if-else-if statements are often used in Dart to introduce the conditional implementation of statements based on one or more Boolean expressions.

The syntax within the example of the Dart If statement, If-Else statement, If-Else-If ladder, and nested If-Else statement will be covered in this tutorial.

How to use the conditionals in the dart in Ubuntu 20.04?

We have demonstrated the representation and implementation of the If statement, If-Else statement, If-Else-If ladder, and nested If-Else statement in the following dart examples.

Example # 1: Using the if condition in a dart in Ubuntu 20.04:

The if statement simply searches the condition and executes the statements inside it if it is true; otherwise, the statements are ignored in the code.

This example starts with the main function where we have implemented if conditional statement. First, we have defined a variable “myNumber” which has the integer value stored in it. After that this variable is utilized inside the if condition. The if has the condition that the variable “number” should be greater than the number “20”. As we have the number “30” greater than the number “20” so the if block returns a print statement. If in case our condition becomes false, then nothing will be executed.

The true statement of if-condition is executed as follows:

Example # 2: Using the if-else condition in a dart in Ubuntu 20.04:

This type of statement checks the condition and executes the statements contained within if it is true; otherwise, the statements contained within else are executed.

If the Boolean expression inside the “if” is true, the script inside the if block is executed, and further execution proceeds with the conditions next to the if-else block.

If the Boolean expression next to the if keyword returns false, the script inside the else block is executed, and the statements next to the if-else block are executed.

In the above dart script, we have first defined the main function. The main function has the integer type variable declared as a “number” to which we have assigned a numeric value. Through the print statement, we have displayed the number inside the variable. Then, we have the if-else representation. The if has the condition given that the variable “number” should be greater than “20”. Inside the if block, the print statement will be executed upon the condition that returns a true value. If the condition returns a false value, then else block will be executed and the if block will be ignored.

As the variable “number” has the value “15” which is not greater than the number inside the if the condition is “20” so the if condition becomes false here. Hence, the else block is executed as follows.

Example # 3: Using the if-else-if ladder condition in a dart in Ubuntu 20.04:

If-Else-If ladders can have a ladder of else-if blocks, but only if a block is required which is at the start and one else block at the optional end.

The Boolean expressions are checked one by one during the execution. If the Boolean condition is true, the associated block of statements is executed; otherwise, the program control moves to the next Boolean in the ladder to be evaluated. The else block is executed if either of the Boolean evaluates is true.

The program has the main function definition where at the initial step, we have constructed a variable as “numeric_val” with the data type “int”. Then, we have the ladder of the if-else statement. The first statement is the if-statement where the condition is defined as the numeric_val Ilesser than the number “5”. If that condition is true, then our first if-condition is executed. Similarly, it considers the second if condition. If it is true, it executes the statements within its block and moves control to the next statement; otherwise, it checks another if condition. Finally, if no if-condition evaluates to true, the statements within the else block are executed and control is passed to the next statement.

From the above if-else ladder, condition2 is true so the if-condition block is executed on the shell of Ubuntu as follows:

Example # 4: Using the nested if-else condition in a dart in Ubuntu 20.04:

In this dart script, we have the variable “Age” of int data type and the variable contains the integer value within the dart main function. Then, we have the if expression, and the if expression is passed with the condition that “age” should be greater than the number “20”. Inside the if block we have first incremented the variable “Age” and then defined the if-else condition within the existing if expression. If the true results are returned from the nested if expression, then the if statement is executed, otherwise the else block is created for the false returned results. If the main if-condition results are false, then the nested if the condition is ignored and nothing will be executed from the above dart script.

As our main if-expression has the true results so the condition is entered into the if-condition block where we have if-else expressions. Inside the if expression our condition fails so the else is executed in the below shell.

Conclusion:

Coding without conditionals forces you to think outside the box. You will have to find new ways to frame your code to try and make it more understandable. It can also assist you in gaining knowledge about computation and/or object-oriented approaches. We have driven all the conditional exists in the dart programming language with the example. We are hoping that there will be no uncertainty with the dart conditionals.



Source link


An interface in the Dart programming language is considered as a way of creating abstraction within your code. The Dart interface acts as a blueprint for all the classes that wish to implement this interface. Afterwards, these classes are required to provide their own implementations for all the interface methods. In this article, we will teach you the use of the interfaces in the Dart programming language in Ubuntu 20.04.

Main Properties of the Interfaces in the Dart:

Following are the main properties of the interfaces in the Dart programming language:

  • There is no direct method of declaring an interface in Dart. However, the simple class declaration can serve as a means of declaring a Dart interface.
  • A Dart class can implement multiple interfaces at a time.
  • The usage of the interfaces in the Dart programming language can help us in achieving multiple inheritances.

How to use the Interfaces in the Dart in Ubuntu 20.04?

To learn the usage of the interfaces in the Dart programming language, you will have to understand the examples explained below:

Example # 1: A Dart Class Implementing another Dart Class:

Since we know that the Dart programming language does not have a direct way of declaring the interfaces, therefore, in this example, we will be creating a class that implements another class to mimic the functionality of an interface. The following Dart script has been written in this regard:

In this Dart script, we have defined a class named “Organization” which will act as an interface. This class has a single member function named “orgName()”. It will not return any value nor will it accept any input parameters. However, this function only has a “print” statement inside it. After designing this class, we have defined another class named “Employee” that implements the “Organization” class. It means that the “Employee” class must implement all the methods of the “Organization” class along with the rest of its functionality.

Inside the “Employee” class, we have declared a member variable named “empName” that refers to the name of the employee. Then, we have a function named “empInfo()” that is used to print the name of the employee on the console. After that, we have implemented the interface method “orgName()” within the “Employee” class. Then, inside our “main()” function, we have created an instance of the “Employee” class named “E1” and have initialized its “empName” variable by using this instance. Finally, we have called both the methods of this class or its own member function as well as the method of the interface implemented within this class by using its object.

To execute this Dart script, we have used the command given below:

$ dart run interfaces.dart

The output of our first example Dart script is shown in the following image. The most important thing to be noticed in this output is that the message defined in the “orgName()” function of the “Employee” class has been printed on the terminal instead of the present inside the interface class. It means that the “Employee” class has successfully implemented the interface class or the “Organization” class.

Example # 2: The Missing Implementation of the Interface Method in a Dart Class:

This example is just a slightly modified version of our first example. Through this example, we want to teach you what happens when you skip the implementation of any of the interface methods inside the class implementing this interface. To do this, we have used the same Dart script that we have designed for the example above. However, this time, we have commented out the implementation of the “orgName()” function within the “Employee” class as shown in the image below:

The rest of the script is intact. After making this modification, when we tried to execute our Dart script, we received the runtime error shown in the following image:

This error message explicitly states that we have skipped the implantation of an interface method within a class that implements this interface. It means that we cannot execute this Dart script unless we provide an implementation of this interface method within the class that implements this interface. Therefore, it is mandatory to provide the implementations of all the interface methods inside the class that implements a specific interface.

Example # 3: Implementing Multiple Interfaces within a Single Dart Class:

The concept of using the interface in the Dart programming language can also be extended to multiple interfaces. A Dart class can easily implement multiple interfaces at a time. The Dart script shown in the image below has been written to demonstrate this:

In this example, we have defined two interfaces in Dart: Organization and Manager whereas the class “Employee” implements both of these interfaces. The “Organization” class has only one member function named “orgName()”. Similarly, the “Manager” class also has only one member function named “designation()”. The “Employee” class that implements these two interfaces has its own member variable and a member function.

Apart from this, it also implements the two methods of the interfaces “Organization” and “Manager”. Then, in the “main()” function, we have just created an object of the “Employee” class and have accessed all of its member variables and functions with this object.

The output of this Dart script is displayed in the following image. It implies that we have successfully implemented multiple interfaces while using the Dart programming language in Ubuntu 20.04.

Conclusion:

With the help of this tutorial, we wanted to explain to you the usage of the interfaces in the Dart programming language. We first began with a brief introduction to the interfaces in Dart followed by a few main properties that are associated with the Dart interfaces. After that, we shared with you three different examples that implemented the Dart interfaces. By going through these examples, you will quickly be able to understand the usage of the interfaces in Dart.



Source link


A set in the Dart programming language is defined as a collection of multiple elements of the same data type. However, each of these elements needs to be unique. In this guide, we will study the usage of the Dart sets in Ubuntu 20.04 by going through some simple yet helpful examples.

Using the Sets in the Dart Programming Language in Ubuntu 20.04:

We have implemented the following seven examples with you to teach you the uses of the sets in the Dart programming language. Each of these examples will introduce you to a new concept associated with the usage of the sets in the Dart programming language.

Example # 1: Declaring a Set by using the Variable Notation in the Dart Programming Language:

There are two different ways of declaring a set in the Dart programming language. We will share with you the first method of doing so. For that, you will have to go through the Dart script shown in the image below:

We have first declared a set named “months” within our “main()” function by making use of the variable notation by using the “var” keyword. This is a “String” type set and contains six different months as its elements. Then, we have just used the “print” statement for printing these elements on the terminal.

To run this Dart script, we have used the following command:

All the elements of our Dart set are shown in the image below:

Example # 2: Declaring a Set by using the Set Notation in the Dart Programming Language:

In this example, we will be sharing with you the second notation for declaring a set in the Dart programming language. For that, you will have to take a look at the following Dart script:

In this Dart script, we have declared the same set of “months” that we have used in the first example. However, this time, we have declared it by using the “set” notation by making use of the “Set” keyword. After declaring and initializing this set, we have just printed its elements on the console.

Upon executing this Dart script, all the elements of our “months” set appeared on the terminal as shown in the image below:

Example # 3: A Set Containing Duplicate Elements in the Dart Programming Language:

Now, when you have learned the two different declarations of the sets in the Dart programming language, we will try to move a step further. We will teach you what happens if you have duplicate elements within a Dart set. For that, you will have to consider the following Dart script:

In this example, we have declared the “months” set in the Dart programming language. However, this time, it contains two duplicate values (i.e. March and September.) To check what happens to this set because of the existence of these duplicate values, we have used a “print” statement for printing the elements of this set on the console.

The output of this Dart script is shown in the image below which tells us that all the duplicate elements present within a Dart set are printed only merienda.

Example # 4: Adding an Element to a Set in the Dart Programming Language:

In this example, we will teach you how you can add an element to an existing set in the Dart programming language. For that, you need to look at the following Dart script:

In this script, we have used the same “months” set that we have used in the examples above. However, this time, we have added a new element to this set by using the “months.add(“May”)” statement. After this modification, we have printed the elements of this set on the terminal again.

The output of this Dart script is shown in the image below. This Dart script has correctly printed the elements of the set after adding a new element to it. Moreover, all the newly added elements to a set are always inserted at the end.

Example # 5: Removing an Element from a Set in the Dart Programming Language:

You can also remove a particular element from a Dart set at any time you want.

In this example, we have just used the “months.remove(“January”)” statement for removing the month “January” from our set. For verifying this removal, we have printed the updated elements of the set on the terminal.

The output of this Dart program is shown in the image below that confirms that our specified element has been successfully removed from our set.

Example # 6: Checking the Length of a Set in the Dart Programming Language:

You can also check the length of a Dart set by making use of the Dart script that follows:

In this Dart program, we have just used the “${months.length}” notation for printing the length of the Dart set on the terminal.

The length of our Dart set in this particular example is “4” as shown in the image below:

Example # 7: Accessing a Particular Index of a Set in the Dart Programming Language:

If you wish to access any particular element or index of a Dart set, then you will have to proceed as explained in the following Dart script:

In this example, we wanted to access the element at the 0th index of our set for which we have simply used the “${months.elementAt(0)}” notation. You can replace “0” with any other index of your choice depending upon the index of the set that you wish to access.

The element present at the 0th index of our set is shown in the image below:

Conclusion:

This article was written to demonstrate the usage of the Dart sets in Ubuntu 20.04. We tried to explain this concept to you with the help of seven simple examples. After going through these examples, you will not only get ordinario with the declaration of the Dart sets but you will also be able to manipulate them by making use of the different associated functions.



Source link