Dart a beginners course

Packtpub

Starting with this video from PacktPub, because I want to develop an App or maybe even a complete Platform. For Flutter it stated that it would be very handy if you knew Dart all-ready. Or an other OOP language. I have worked with Java for a while, but the whole OOP concepts didn’t sink in really. That’s why I started with this video course that is less then 2 hours! That is a good starter.

So DartPad is the online testing IDE to go to. Some examples were easy to execute:

first thing to notice is the first print statement. It was written differently then I am used to. The seconde print is the old fashion way I learned.

The first video stated: go to this webiste (fartlong.org, dartlang.org) but got redirected to an other website. That is the moment, you realize that you are watching an old video (2018…ancient in IT years ๐Ÿ˜‰ )!

This became:

Select the (for me right on) Linux download instructions

follow the steps and:

Than the next statement was quite long, that is why I paste it in here:

wget -qO- https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo gpg --dearmor -o /usr/share/keyrings/dart.gpg

another long command to add a repo:

echo 'deb [signed-by=/usr/share/keyrings/dart.gpg arch=amd64] https://storage.googleapis.com/download.dartlang.org/linux/debian stable main' | sudo tee /etc/apt/sources.list.d/dart_stable.list

After this you are just ready to begin the installation, with 2 commands:

 sudo apt-get update
$ sudo apt-get install dart

134 MB download start and version 2.16.2-1 is installed on my VM … oh snap! I wanted to make a backup of my VM that is running on the home build NAS with UNRAID (works so freaking nice!)

I saw dart-doc also, so just to know that I downloaded the information, I installed the doc part too!. Seems to be more a reporting feature, but we can explore that later!

the commands to add dart to your $PATH seems needless to show ๐Ÿ˜‰

for the download I looked into the App Store (or how it is called in Ubuntu) and found this one, stable release 2202.1

What version is on the website?

same version! So going without the extra hassle and install it in the app.

So now this video tutorial is taking much more time than expected. But I ended this after half an hour :-). Instead of the minutes it would take.

Section 2 is about setting up the IDE so starting IntelliJ IDEA

The video didn’t show the you have to agree to all our shit things list, but I did.

The settings screen is so different so here was my screenshot:

because the video showed that Plugins was entered through settings, I tried that. Later I looked a bit better and saw the big Plugin part on the main left side of the screen. Type Dart and install. IDE restart doesn’t show for me, so try without the time consuming restart ๐Ÿ˜‰

 

To see if it works: “New Project” –> is there a Dart logo to create a Dart project –> you have passed this incredible test: congrats

 

Oh no … the video states that you have to have an SDK installed. Of course .. no wait…what? Where did I do that?? wait … that was where the restart was for.

So lets restart the IDE.

And … still no SDK. what the f*dge did I miss something? I clicked the link in the plugin information that said: more information about using this plugin in your IDE … that link showed that you have to install the Dart SDK manually. That is not what the video showed. Also it showed that the PATH was a Flutter installation, so it might be a little different than my clean install.

oh crap … that was the same page I used to install Dart yesterday. It states that the dart/bin directory is the one I neededย  *sigh* .. if only i would have read it a little better yesterday.

notice that I had to remove the bin directory. Otherwise an error showed up:

After this I should be able to create a project, but I cannot choose what type of application I can create with my Dart installation ๐Ÿ™

HelloWorld does not work the way they showed! After being away for a while and sending my wife to the vet (I am afraid that our young ragdoll cat is very sick ๐Ÿ™ ). She went away and will be for the next couple of hours. So I tried to get a new Project and all of a sudden there are multiple application options:

In the example is the console application. That makes sense, because you can see the output in your IDE, leaving the checkbox checked will give you sample data and application stuff.

Damned … still got no main.dart helloworld thingy ๐Ÿ™

So I deleted the directory in Linux Files and then started all over. Now its more like the video:

Then I wanted to use my HelloDartWorld naming convention again, and again it didn’t give me my dart.main file. That is weird. I will delete it once again and create another project with only lowercase:

That works … lowerCamelCase? Test: again without any luck. So the lowercase-project-shaming-naming is a thing to remember!

So it showed some amazing stuff, Mike. Do tell us more! We want MORE ๐Ÿ™‚

in the default main.dart there are multiple things to look at:

comments are added with // for single line or /* comments on multiple lines */

After this example, we are guided into the variable’s world! Exiting? Nah .. but let’s build it!

Create another project named boelens (a teacher of us was mr Boelens … instead of the bools name in the video. Remove the package import and add comments to the starting point:

Another funny thing is that the given example isn’t working anymore. It seems that to many programmers didn’t assign their variables and that isn’t allowed anymore. In the example it prints a null and in my example the code won’t run:

it shows exactly where the problem arises and makes it more practical for bad programmers as me-myself-and I

After that it was shown that the boolean var could be changed from false to true .. and with the method ${isOn.runtimeType} it showed that it indeed was a boolean.

But after changing from the specific bool Type to the generic var Type (it can be anything!) it still showed as bool, because Dart detected the value and interpreted it as a boolean. After surrounding it with quotes, it changed to type String:

with the output:

The next section is about numbers num is the generic part and it can either be an int (12) or a double (12.2)

Strings are just a bunch of characters. But the example shows some stuff about getting the index of a part (space) and do substring(from_that_index).trim(); to remove spaces. I am going to test what it does…

you get get the length of a string and search if it contains a string or character.

and indeed if I run the following code:


with spaces

without spaces

Note:ย the examples for printing statements are as follows:

print(‘lastName is: ${lastName}’ );

That is somehow different than what I was used to (concat to get the same results).

print(‘lastName is: ‘+lastName );

but the IDE gives a warning that you should remove the unused brackets and that is done by a simple key combination.

Alt + SHIFT + ENTER does the removal! Worked like a charm!

a usefull command is to create a list out of a string:

you can print the wanted items out of this list by using a index (starting from 0, all the way to be the Dart hero!)

 

working with constants:

use const String name = ‘Dwight’;

name = ‘Dikke Dwight’ will give an error, because a const is not mutable

using stdout for printing you have to add \r\n to do a return and newline feed

the example for reading user input does not compile and work:

There are checks for the possiblity to be null … it gives a couple of possible fixes: make the string nullable String? … but that does not do the trick. So this video makes me look at Google to fix the chages that have been added to the Dart language. I guess ๐Ÿ˜‰

@ StackOverflow the solution was desribed as shown in the picture above this. Make the String nullable. But then the name.isEmpty() must be made nullable .. but the there is a error that a bool? cannot be compared with a bool *sigh*

This is called null safety, so remember that!

So this code works. But it looks like I had to change the isEmpty ? with a second question mark? Lets try that:

NOPE!

collections!

Enum has to be declared outside the main function

voorbeeld was om enum te laten zien, zodat je kunt zien dat de variabele printen niet gewoon lukt. Je moet naam.values gebruiken. Dat geeft een lijst terug

Prima om zo te kunnen werken. Zo kun je data doorsturen?

 

Leave a Reply

Your email address will not be published. Required fields are marked *