Categories
Dart

Dart Variables

Variables are one of the most important concepts in the programming languages.

They are named memory spaces or temporary assignments for storing values in the program.

The name provided to the variable is known as identifiers.

For ex :

int A=5;

Here, A is a variable storing the reference to the int object with value 5. and A is also the identifier name provided to the variable to be recognized.

Variable Naming Convention Rules

We can assign any name to our variables following some predefined naming convention rules as below,

  • Variable names can contain Alphabets and Numbers.
  • Variable names cannot start with Numbers.
  • They cannot contain spaces and special Characters except dollar ($) and underscore (_) sign.
  • They cannot be the same as reserved keywords like if, for, switch, etc.

Why do we have to follow the naming convention rules?

As we all know that compiler or interpreter can compute or manipulate data much faster than humans. but, they don’t have the capability to think on their own. so, if you assign your variable name as reserved keyword like,

For ex:

int for=0;

So, the compiler will not be able to differentiate between the variable name for and for loop. So to resolve the confliction/ambiguity there are certain rules predefined for naming an identifier.

So, here is the list of predefined keywords which cannot be used as variable names are as following

abstractelseimportsuper
asenuminswitch
assertexportinterfacesync
asyncextendsisthis
awaitextensionlibrarythrow
breakexternalmixintrue
casefactorynewtry
catchfalsenulltypedef
classfinalonvar
constfinallyoperatorvoid
continueforpartwhile
covariantFunctionrethrowwith
defaultgetreturnyield
deferredhidesetdo
ifshowdynamicimplements
static

Variable Syntax

A variable must be declared before it is used in Program.

var the keyword is used for declaring the variable data type dynamically in dart or you can also declare specific data type before the variable name to assign the value.

For ex :

var variableName=”Shiva”;

Var is the best way to declare a variable without specifying its data type.

All variables in dart stores references to the objects or values.

For ex :

int number=10;

Here variable stores the reference(address in memory) to the identifier number containing value 10.

Most of the Languages Supports Type Checking by prefixing the data type with variable name. Type Checking ensures that variables declared with specific data type stores only the values to that specific data type.

For ex :

String name=”Billy”;

Here variable name can contain only String data type values. user cannot assign int or boolean value to String data type variables.

So by prefixing var keyword user do not have to define the data type for the variable. variable will predict the data type which type of data is assigned to variable.

For ex :

var x = 10;

Now x will act as the Int data type and it will have all the properties and methods of Int data type. var y=”hi” similarly, here y will act as the String data type and it will have all the properties and methods of String data type.

So var keyword is used when you do not know which kind of data will be stored in the data type. So at the time of variable declaration using var keyword will help you to get dynamic data type at runtime.

Initial Values

Variables declared but not initialized in dart have initial values null.

For ex :

void main()
{
	var a;
	int b;
	boolean c;
	print(a);
	print(b);
	print(c);
}

Output :

null
null
null

Final Variable

Final variable is the variables which should be initialized at the time of variable declaration.

The value of the final variable can be set only once. we cannot change the value of the final variable at any time.

The final variable doesn’t have any default value. So, the final variable must be Initialized.

When you use a final variable you do not need to define the data type of the variable. final will act as the data type which is assigned to variable.

For ex :

void main()
{
  final value="Hi";
  final value2=101;
  print(value);
  print(value2);
}

Output :

Hi
101

Const Variable

Constant variable is used to store the constant value in the variable.

It must be initialised at the time of declaration.

Also the value of the const variable can be set only once. we can’t change the value of const variable at any time.

The const variable doesn’t have any default value. so, the const variable must be Initialised.

When you use const variable you do not need to define the data type of the variable. const will act as the data type which is assigned to variable.

For ex :

void main()
{
  const value="Hello";
  const value2=102;
  print(value);
  print(value2);
}

Output :

Hello
102

The only difference between final and const is that we can assign the final variable any other value while const variable can have only constant variable. You cannot assign any other value to it.

For ex :

void main()
{
  for(int i=0;i<10;i++)
  {
    final value=i;
    print(value);
  }
}

Output :

0123456789

For ex :

void main()
{
     final i=0;
     const value=i;
     print(value);
}

It will give you a compile time error. because constant can have only compile time values (i.e., values assigned at the time of variable declaration). You cannot assign any value that can change at run time.

So these are the different usages of variables.

Feel free to ask your questions in the comment section. Keep reading and commenting your suggestions to make toastguyz a better site to learn different programming languages.

Categories
Dart

Dart Data Types

Data type in any language is a classification of data which tells the compiler or interpreter how the programmer wish to use data.

Data types are used generally in type safe languages (Type safety is the concept of preventing type errors while accessing different kinds of data types based on authority of access).

Dart 1.X used optionally type checking while Dart 2.X uses a combination of static type checking and runtime checks to ensure that a variable’s value always matches the variable’s static type.

The Dart supports following built in data types:

  • Numbers
  • Strings
  • Booleans
  • Lists
  • Maps

Numbers

Dart number has 2 subtypes,

  1. Integer
  2. Double

int

Integer data type can have any value from natural numbers and their negative counterparts(i.e., …-3,-2,-1,0,1,2,3,…)

int keyword is used to declare integer data type variable.

For ex :

int a = 7;

double

Integer data type cannot accept decimal values so, double data type is Introduced with 64 bit range which accepts all the decimal values from range …-3,-2,-1,0,1,2,3,…

double keyword is used to declare double data type variable.

For ex:

double a = 7.7;

Strings

Strings are nothing but sequence of characters or bunch of characters. It can be used to store any number of characters like, for storing name, personal details, different kind of information etc.

String keyword is used to declare string data type variable.

For ex :

String a = “Ram”;

Boolean

Boolean data type is used whenever we want to apply conditional test on our logic. It can have only two values True or false.

bool keyword is used to declare boolean data type variable.

For ex :

bool x = 2>3; // so, x has value false.

Lists

list is nothing but the collection of different elements of the same type or collection of ordered group of objects.

So, list is used to store elements of particular kind of data type or to store objects.

In dart arrays are not used. instead dart uses arrays as list-objects.

For ex :

void main(){
	List<int> a=new List();  // int is  type of data
	a=[1,2,3,4,5];
	print(a);
} 

Output :

[1,2,3,4,5]

Maps

Maps is one of the best data Structure for storing the data in key-value pairs.

Each key is associated with one value in Maps. Both keys and values can be of any type for mapping.

For ex :

void main(){
	Map<String,String> a=new Map();
  	a={'Username':'BillGates','Password':'123456'};
 	print(a);}

Output :

{Username: BillGates, Password: 123456}

Dynamic Keyword

As we all know that dart is optionally typed language which means statically typed as well as dynamically typed language.

Statically typed language is one in which the data type of the variables or parameters are defined at the time of compilation or say declaration.

For ex :

String, Boolean, Map etc.

While Dynamically typed language is one in which the data type of the variables or parameters are defined at runtime. So, dart has dynamic keyword for declaring variable data type at runtime.

For ex :

void main()
{
    dynamic x=5;            // x will be evaluated at runtime as integer variable having value 5.
    dynamic y="Toastguyz";  // y will be evaluated at runtime as String variable having value "Toastguyz".
    dynamic z=true;         // z will be evaluated at runtime as boolean variable having value true.
    print(x);
    print(y);
    print(z);
}

Output :

5
Toastguyz
true

In this way, If you’re are not sure about the type of variable you can declare it as dynamic which will be explicitly evaluated based on value assigned to it.

So, these are the basics of for each data types. You must deep dive in to dart data types to quickly grasp the dart language.

Feel free to ask your questions in the comment section. Keep reading and commenting your suggestions to make toastguyz a better site to learn different programming languages.

Categories
Dart

Dart Syntax

For every programming language, there are certain rules are predefined for writing programs.

Dart also have its own syntax or structure for writing programs

A program written in dart may be consist of

  • Operator
  • Variables
  • Functions
  • Expressions
  • Class
  • Objects
  • Constructors
  • Libraries
  • Typedef
  • Data Structures represented as Collections/Generics
  • Comments

We will understand each and every topic of dart through step by step to have command over dart language and smoother our experience of learning dart syntax.

Let’s have a basic hello world example in dart,

Helloworld.dart file

main() {
print(“Hello World”);
}

In Dart, The execution of your program starts with main() function. main() is the entry point function which is mandatory in a dart program.

Any dart code written in a file must have extension have “.dart”. In above example, we have created a new file named as Helloworld with .dart extension and written our program inside it.

With the .dart extension file we can identify that the particular file has code written in the dart language.

Similarly, there are other syntactical concepts or rules that will help you to understand the dart syntax easily.

Let’s have intro with each dart syntactical rules one by one.

White-spaces and Line Breaks

We can freely use white-spaces, tabs and new lines as much as we want in our programs and we are able to format our code because Dart ignores spaces, tabs and newlines that appear in code.

Dart is case sensitive

Dart is case sensitive language, so it differentiates lowercase and uppercase characters in your program.

Statements ends with a semicolon

In dart code, every statement must end with semicolon (;). We can write multiple statements in a single line, but we need to add semicolon between them.

Comments in Dart

Comments are useful for any program. It improves readability of our program. They are used to notate the particular code-block or function or class is used for what purpose or helps other programmers to understand our code by reading comments.

In dart, two types of comment are there:

  • Single Line Comment : Any text between // is comment.
  • Multi Line Comment : Any text between /* */ is multiline comment.

For ex :

// This is example of single line comment.

/* Line 1
Line 2 –>This is example of multiline comment.
Line 3 */

Dart syntax is conventional programming syntax like C, C++ and java. Hence, easier to learn and understand. So, you can have command over the dart language in very short time. So, make yourself ready to have a smoother journey for learning an object oriented dart programming language.

Feel free to ask your questions in the comment section. Keep reading and commenting your suggestions to make toastguyz a better site to learn different programming languages.

Categories
Dart

Dart Environment Setup

After checking the dart overview, if you’re interested in learning dart then you’re at the right place. we will discuss about all the basic and advanced concepts of dart one by one.

Now you will be thinking about the prerequisites for learning dart language. And only one prerequisite required for learning dart throughout this journey is your willingness and interest in learning dart coding.

To practice your coding skills while learning different concepts, you will require tools or IDE to test your dart code.

So, we can run our dart code in following ways in different environments.

Run In DartPad

Starting from official editors, DartPad is the online editor for testing your dart programs. To test your programs on DartPad visit the official website

The DartPad executes the dart code online and displays both HTML as well as console output.

Run Dart Code with command-line using Dart VM

To run any dart program in your machine we need the support of Dart Virtual Machine (Dart VM) which contains the components for executing dart codes.

We can get Dart VM by Installing the Dart SDK(collection of dart libraries and components run our dart code). To download the current stable version of Dart SDK, visit the official website

Install the Dart SDK and then setup the PATH environment variable to following path :
<your-dart-sdk-path>\bin

Generally, SDK path in windows would be like (C:\Program Files\Dart\dart-sdk\bin).

Now, to verify the installation of the Dart VM write the below command in terminal :
dart

If installation is successful then it will show the dart runtime and details. Otherwise check out the above steps for installing dart SDK in your machine.

To run any program using command line, Create a file with .dart extension like hello.dart :

hello.dart file

void main() {
  print(‘Hello, World!’);
}

Now to run it in command line, Go to the dart file path in terminal and then write the code below,
dart hello.dart

Output :

Hello, World!

Run dart code with IDEs(like Intellij, WebStrom, Android Studio)

Now comes the IDEs which support the Dart as plugin to test our skills. So, there are numerous IDEs which provides support for Dart Plugin. Some of the Famous IDEs are :

  • Intellij IDEA
  • WebStorm
  • Android Studio
  • XCode

Differnt IDEs also requires Installation of Dart SDK in your Machine. So, install Dart SDK as instructed above.

After Installing the Dart SDK, you have to Install IDE of your choice. So, to Integrate Dart with Intellij IDEA Software Download any one IDE of your choice from below links.

Intellij IDEA visit official website
Webstorm visit official website
Android studio visit official website
XCode IDEA visit official website

Now, Install your IDE. Then Install dart plugin in your choice of IDE. Then Click on create a project and select Dart option and then you have to specify the path of your Dart SDK which you have downloaded. Then you will be able to create a project and run dart code as well.

So, in one of the above way you can setup your dart programming environment to test your coding skills while learning the dart language.

If you face any difficulty installing Dart SDK or anything else then comment below and we will help you to sort out your issue.

My personal suggestion to test your dart code is Dartpad or Intellij IDEA. As I really feel comfortable working with these two components.

Feel free to ask your questions in the comment section. Keep reading and commenting your suggestions to make toastguyz a better site to learn different programming languages.

Categories
Dart

Dart Introduction

Dart Introduction / Dart Overview

Dart programming language was originally developed by Lars Bak and Kasper Lund in 2011 at Google.Inc Organization. Dart was revealed at the GOTO conference organized at Aarhus, Denmark, in October 2011.

What is Dart?

Dart is a general-purpose programming language originally developed by Google and later approved as a standard by Ecma organization. It is used to build web, server and mobile applications. It supports optionally typed(Static as well as Dynamic) language with Object Oriented Concepts.

Dart is an object-oriented, strongly typed, single inheritance language using a C-style syntax that transcompiles(compiler that converts the source code of a program into another language.) optionally into JavaScript. It supports classes, interfaces, polymorphism, abstract classes, generics, static typing and strongly typed system.

Dart is a Google’s Replacement to take on Javascript as the Default Programming Language of the web.

Why Dart?

Developers at Google and outside uses Dart to create high-quality apps for iOS, Android, and Web with features targeted for client-side development. Dart is a great fit for both mobile and web apps.

Productive

Dart’s syntax is clear and concise. its tooling is simple but powerful. Dart has feature-rich libraries and an thousands of packages having predefined classes to make your coding easier.

Fast

Dart provides optimizing ahead-of-time compilation to get predictably high performance and fast startup across mobile devices and the web.

Portable

Dart is platform-independent language. So that dart mobile apps can run natively on iOS, Android, and Web. For web apps, Dart transpiles(that generates JavaScript equivalent of a Dart code) to JavaScript.

Approachable

Developers have found that Dart is particularly easy to learn because it has features that are familiar to users of both static and dynamic languages.

Dart is familiar to many existing language as it has conventional syntax structure based object oriented programming. If you already know C++, C#, or Java, you can be easily familiar with Dart in just few days.

Reactive

Dart is well-suited to reactive programming due to Dart’s fast object allocation and garbage collector. Dart supports asynchronous programming through language features and APIs that use Future and Stream objects.

Usages of Dart

Dart is a scalable language that you can use to write simple scripts(Programs) or full featured apps. Whether you’re creating 1) a mobile app 2)web app 3)command-line script, or server apps.

Dart helps you to build beautiful, creative, high-quality, smooth user experience UI by providing flexible support for flutter SDK to create beautiful iOS and Android apps.

Dart compilation is very fast, predictable. Which helps flutter framework to make its components easily customisable.

Dart has fast development cycles and game-changing workflow due to its Just In Time compilation.

Flutter have the biggest advantage over other languages(like java, swift etc) as Flutter doesn’t require a separate declarative language(XML or JSX etc.) required to create an app layout due to dart’s declarative and programmatic support.

All these unique features makes dart very powerful for building Flutter Framework to create beautiful and user-friendly android and ios apps.

Dart code execution

There are three main ways to run dart code.

  • Dart code can run on all major browsers with no need for browsers to adopt Dart. while running Dart code in a web browser the code is precompiled into JavaScript using the dart2js compiler.
  • The Dart SDK also ships with a stand-alone Dart Virtual Machine, allowing dart code/program to run in a command-line interface environment(Command Prompt).
  • Dart code can also be compiled in Ahead Of Time Mode in which, code can be directly converted in to native machine code.

At last, Dart is an interesting and emerging programming language with highly scalable features to support the development of mobile, web and command line apps.

In the case of web applications, AngularDart makes it a very powerful tool to build web apps.

In case of Mobile applications, Dart provides greater support for building android and ios apps from the same codebase.

In case of command line applications, the Dart SDK is constantly updating and developing to provide the greater user support.

Overall dart is a highly scalable and easy to learn a language for beginners while powerful and productive language for developers to build various apps efficiently.

Feel free to ask your questions in the comment section. Keep reading and commenting your suggestions to make toastguyz a better site to learn different programming languages.