Day Two of the #60Days of Flutter Challenge
More about variables, storing inputted information, and converting data types
Day one is officially in the books.
Picking up from where we left off yesterday, let’s continue looking at variables. We’ve covered String variables, so next, we’ll learn about int, double, and boolean ones.
There are two main types of number variables, int and double. Int, short for integer, is for whole/counting numbers. Doubles are for decimal numbers.
In the example, we use int to represent a number of items (which will always be a whole number). We use the double variable to express the price.
Stepping away from number variables, we next look at the different ways of counting. (Addition (+), Subtraction(-), Multiplication(*), and Division(/)) are all potential options. Like anything else math-related, the order of operations is in play — so PEMDAS people.
There are different ways to perform mathematical operations. In the above example, the variable quantity is equal to 300. We can change the variable with simple math:
There are two different ways to do the above. Either quantity = quantity + 50; or quantity += 50;. In either case, the variable quantity, once 300, is now 350.
Each of the fundamental math operations is available to use: add (+=), subtract (-=), divide (/=), and multiply (*=).
Dart also uses the modulo operator (%) to determine the remainder of a number. Printing (11 % 3) would give you an answer of 2.
11/3 = 3 with a remainder of 2. The modulo operator doesn’t have anything to do with the front part, only the remainder.
Booleans, at least at this point, seem simple enough. A boolean can return either true or false — that’s it, no fuss, no muss.
Above is an example of a Boolean Expression. Typically, boolean expressions are used as conditions that point a program toward a particular operation.
For (very basic) example:
if (5>3) then do something else do something else.
Dart cannot do everything “out of the box.” Sometimes it needs some added muscle to perform a particular task or series of tasks. When that happens, you can import a collection library.
Doing that seems relatively straightforward. Type the word import above the main() function (at the top of the screen). Within quotes, type the name of the library you want to use.
It’ll typically be import “dart:something”;
In the above example, we can find the minimum number between 1 and 9 as well as the square root of 144. The terms used, min and sqrt, are part of the dart:math library. Without importing the library, they won’t be recognized, and you’ll get an error message when you try to use them.
The last thing we looked at today was a way to store user input. To achieve this, we need to import the dart:io collection library.
Like most other information, this information is stored in a variable — in this case, a String variable. In the example above, under the question “What is your name?” sits the String variable userName (variables are written lower case for the first word and first letter capitalized for the others — it’s called lowerCamelCase).
Stdin is short for standard input, but the critical part is readLineSync(). The latter reads the information inputted by the user and stores it in the appropriate variable.
In the above example, the program will ask you your name (let’s say Matt). Once you type it out, it will then add Hello Matt.
Occasionally there will be cases that the user information won’t be immediately usable. In the below example, a simple calculator app, the numbers a user enters are in String format. They need to be changed to integers (or doubles) to be mathematically added together.
To change a string to an int or double, you need to parse it. In the example, the strings num1 and num2, when they are added together, are changed to integers using int.parse (or double.parse).
That is it for day two. We have completed about one hour and fifteen minutes of Mike Dane’s YouTube tutorial.
On to day three:
In case you missed it, here is the post for day one: