Variables
Just like JavaScript, A variable can be defined with a var, const and final keywords and concatenate with sign ‘+’, let cant be use for variable it is used to use class return function , Please see below exampleimport "package:flutter/material.dart";
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
const var1 = "Variable 1";
final finalvar1 = "Final var1";
final finalvar2 ;
finalvar2 = "Final var2";
return MaterialApp(
title: "Welcome to Flutter",
home: Scaffold(
appBar: AppBar(
title: const Text("Welcome to Flutter"),
),
body: Center(
child: Text(var1+", "+finalvar1+", "+finalvar2)
),
),
);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Operators
Mostly we use below three operators- Assignment operators
- Arithmetic operators
- Comparison operators
- Ternary Operator
Assignment Operators
Operator | Meaning |
---|---|
-= | subtraction assignment |
/= | divisions assigment |
%= | modulo assignment |
+= | addition assignment |
*= | multiplication |
~/= | integer division assignment |
>>= | bitwise shift right assignment |
^= | bitwise XOR assignment |
<<= | bitwise shift left assignment |
&= | bitwise and assignment |
|= | bitwise or assignment |
Arithmetic operators
Operator | Meaning |
---|---|
+ | Add |
– | Subtract |
-expr | Unary minus, also known as negation (reverse the sign of the expression) |
* | Multiply |
/ | Divide |
~/ | Divide, returning an integer result |
% | Get the remainder of an integer division (modulo) |
++var | var = var + 1 (expression value is var + 1) |
var++ | var = var + 1 (expression value is var) |
–var | var = var – 1 (expression value is var – 1) |
var– | var = var – 1 (expression value is var) |
Comparison operators
Operator | Meaning |
---|---|
== | Equal |
!= | Not equal |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
Ternary Operator
Ternary operators can be defined as a conditional operator that is reasonable for cutting the lines of codes in your program while accomplishing comparisons as well as conditionals. This is treated as an alternative method of implementing if-else or even nested if-else statements. This conditional statement takes its execution from left to right. Using this ternary operator is not only an efficient solution but the best case with a time-saving approach. It returns a warning while encountering any void value in its conditions.Operator | Meaning |
---|---|
(( 3>2)?'Greater':'Less') | return string “Greater” |
(( var1>finalvar1)??'Less') | return true ,false |
(( var4)??'Test') | return true if value is exists for variable var4 else print Test |