Question #22MediumGeneral

`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 = 0
is an integer — you cannot reassign it to a
text
String
.


Example

dart
int 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
text
dynamic

dart
dynamic 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
text
Object?

dart
Object? A = 0;
A = 'name'; // ✅ Any non-null type
A = null;   // ✅ Null also allowed

Dart Type System

DeclarationCan change type?
text
int A = 0
❌ No — fixed to int
text
String A = 'hi'
❌ No — fixed to String
text
dynamic A = 0
✅ Yes — any type
text
Object A = 0
✅ Yes — any non-null type
text
var A = 0
❌ No — inferred as
text
int
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 —

text
int A = 0
cannot be reassigned to a
text
String
. Dart is statically typed. Use
text
dynamic A = 0
if you need a variable that can hold different types.