`Int A =0;` on pressing the button can we change `A=‘name’;`?
Answer
Answer
No. In Dart, which is statically typed, you cannot change the type of a variable after it's declared.
text
int A = 0text
StringExample
dartint A = 0; // ❌ Compile-time error — type mismatch A = 'name'; // Error: A value of type 'String' can't be assigned to a variable of type 'int'
Options If You Need Dynamic Types
Option 1: Use textdynamic
text
dynamicdartdynamic A = 0; // Start as int A = 'name'; // ✅ Now a String — dynamic allows any type A = true; // ✅ Now a bool print(A); // true
Option 2: Use textObject?
text
Object?dartObject? A = 0; A = 'name'; // ✅ Any non-null type A = null; // ✅ Null also allowed
Dart Type System
| Declaration | Can change type? |
|---|---|
text | ❌ No — fixed to int |
text | ❌ No — fixed to String |
text | ✅ Yes — any type |
text | ✅ Yes — any non-null type |
text | ❌ No — inferred as text |
dart// var infers the type — locked after inference var A = 0; // Inferred as int A = 'name'; // ❌ Error — A is still int // dynamic — truly flexible dynamic B = 0; B = 'name'; // ✅ Works
Answer: No —
cannot be reassigned to atextint A = 0. Dart is statically typed. UsetextStringif you need a variable that can hold different types.textdynamic A = 0