Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define variable and constant in flutter?

as we define in swift

constant with let

variable with var

how to define in flutter?

like image 297
Vishal Shelake Avatar asked Sep 16 '25 09:09

Vishal Shelake


1 Answers

Variables:

var number = 42;

Constants:

If you never intend to change a variable, use final or const, either instead of var or in addition to a type. A final variable can be set only once; a const variable is a compile-time constant. (Const variables are implicitly final.) A final top-level or class variable is initialized the first time it’s used.

final name = 'Bob'; // Without a type annotation
final String nickname = 'Bobby';

Use const for variables that you want to be compile-time constants. If the const variable is at the class level, mark it static const. Where you declare the variable, set the value to a compile-time constant such as a number or string literal, a const variable, or the result of an arithmetic operation on constant numbers:

const bar = 1000000; // Unit of pressure (dynes/cm2)
const double atm = 1.01325 * bar; // Standard atmosphere

More here: https://dart.dev/guides/language/language-tour#variables

like image 106
Yash Avatar answered Sep 17 '25 23:09

Yash