Tuesday, August 24, 2021

How to Code in Dart Programing language? Dart Hello World Example Tutorial

Google Datt is a general-purpose programming language from Google and is used to build web applications, mobile applications, and the Internet of Things (IoT).  Its most popular application is in Flutter framework, which is Google's mobile app SDK for crafting high-quality native interfaces on iOS and Android in record time. If you haven't tried it yet then it's a good time to try it and see how much it offers to a programmer and whether it is a suitable language to replace JavaScript or not. 

As a Java Programmer, my first impression of Google dart is great. It looks quite familiar with Java my favorite programming language and this motivates me to write this hello-world example in Google Dart and execute it, run it and see how it works.

Similar to Java, Google dart also has Class and Object, it has a final keyword, String Class, Object class, extends keyword, and many more I going to learn but it really looks structure because it has google dart compiler which shows a warning and error like any other structured programming language.

In this Google Dart tutorial, we will see How to write HelloWorld in Google Dart and How to run the Google dart program using Google Dartboard Application which is a browser-based application, allow you to write and run Google Dart Programs. This is the easiest and quickest way to get a feel of Google Dart language itself before trying it with HTML code.

The Google Dart also has a compiler and HTML converter which allow you to convert Dart code into JavaScript and use it. We will see about how to convert Google Dart Code into JavaScript in next article. For now, let's start with this step by step guide of writing Google Dart HelloWorld Example and running it.




Step by Step Guide to Write and Run Google Dart Example

Here is step by step guide to writing your first program in Google Data Programming language. It's none other than the class HelloWorld program, which you might have already written for Java, Python, or C++.

1. Open DartPad in your browser (https://dartpad.dartlang.org/)

Make sure you use Mozilla Firefox, Google Chrome or Apple Safari. Google DartPad application won’t work on Microsoft Internet Explorer, though support is coming in maybe few days or week.

Once you open this link Google DartPad Application will be open on your browser and the first example which it has is "Hello " as shown in below screen.


Google Dart helloworld example




2. Important points about Google Dart Structured Programming language


Now you have two options either edit the existing program or write a new program. Before writing new program let’s see few points about google dart language.

1. Like Java google dart program will start with main () method.


2. You can declare Variable using "var" a JavaScript style or use a correct type like String or int using Java Style, for example below two code are same.

String text = "NYSE is biggest Stock Market in the world";
var text = "NYSE is biggest Stock Market in the world";

It depends upon your choice whether you want to adopt javascript style or Java Style. Since I am a Java programmer I found Java Style more intuitive and easy to use. 

By the way, finally, var is now also available in Java 10. If you like to write concise code in Java, you can consider using var to declare local variables instead of declaring a full type name. 



3. You can assign an incorrect type to variable e.g. in this example assigning "text" as int, your programme will run but Dart compiler will show warning related to incorrect type.

int text =" google dart compiler will show a warning but the programme will run".




4. Instead of var or a formal type, you can use final keyword and your program will run successfully.


5. Each statement ends with a semicolon ";" otherwise the compiler will throw error "Unexpected token "IDENTIFIER expected" or "; expected"


6. For displaying the value of the variable we need to use interpolation or jsp expression language style "${var}" or else it will complain as "cannot resolve text".


7. We can display the string as literal, exactly same as in Java. The print() method is similar to Perl, Java or any other language. For example

print("This is String literal");




3. Our first Google Dart Program


main() {
  String tradingSecrets = "High Frequency Trading requires extremely fast computers";
  int latency = 10;
  String message = "Google Dart language";
 
  print('Hello world in ${message }');
  print("This is String literal just like Java");
  print( '${tradingSecrets}');
}


Output:

Hello world in Google Dart language
This is String literal just like Java
High-Frequency Trading requires extremely fast computers



IV. Running your Google Dart program

Just press the Run button as shown in the above image, it will contact the Dart server and run your program, sometime you may get an error as "The darn server failed to respond." but mostly you see out of your google dart program.

This is the first Google Dart tutorial there are more to come but this is good to start and get feeling of Google dart language. Google Dartboard application is easy to use and execute google dart program which reduces learning curve but converting google dart program to JavaScript and then running into HTML file like JavaScript requires little effort which we will see in next article.


Thanks for reading this article so far. If you like this Google Data Tutorial then please share with your friends and colleagues. If you have any questions or feedback then please drop a note. 

1 comment :

Anonymous said...

Syntax of Google Dart language is similar to Java. looking at those extends, final , Class keywords no doubt syntax is inspire by Java and C# and targeting those developer to adopt google dart.

Post a Comment