Initially, I instantiated date01 object in a class, but sometimes the results used to stick despite input had changed. To fix that, I moved the object instantiation of class AjtDateUtils into onPressed or more precisely into setState as follows:
onPressed: () {
setState(() {
AjtDateUtils date01 = AjtDateUtils();
result =
date01.function_call(firstController.text);
});
},
child: const Text('Compute'))
Now, I'm curious if this is good or bad. Will this result in incremental memory usage on every button press, or this will be handled properly by the language (destroyed before each next instantiation)? Or, do I have to implement some more code to handle something I'm unaware about?
UPDATE 01:
To test, I created the following code:
import 'dart:io';
class SomeClass {
var myList = [
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
];
}
void main() {
int response = -1;
while (response != 0) {
stdout.write("Input Integer: ");
response = int.parse(stdin.readLineSync() ?? "");
for ( int i = 0 ; i < response ; i++ ) {
SomeClass sc01 = SomeClass();
}
}
}
Ran the above code, noted the initial reading in linux utility top, then entered 10000 to instantiate 10000 times, then again noted the readings for VIRT, RES, and SHR memory column for the pid of the dart code. I repeated the test 4 times, and then changed:
SomeClass sc01 = SomeClass();
to:
SomeClass? sc01 = SomeClass();
sc01 = null;
And repeated 4 similar tests with this code. The result is as in the following screenshot:
What conclusion should I draw from this? (Or, is this insufficient/inefficient?)

Accessebility
No doubt it's preferable to have an access to an object through the entire class, to be a class member not just a local variable. that choice can be made depending on the use case and where that object will be used.
Memory Allocation
Dart is a garbage-collection-based programming language, once the object get free (the object is not referenced any more) it will be destructed eg: removed from the memory. This is the the responsibility of the operating system to manage its memory.
Note: Dart doesn't have destructors.
now, your question, if it's mandatory to use that object as a local variable but you care about the memory:
it's ok. try to clear the reference to that object after done with it, so it becomes a garbage.
you can unreference an object by setting its reference variable to null
Note: dart is a null-safety and sound-null-safety language. so dart analyzer may not accept a null value.
if so, you can make a dispose method as a member method of the object wanted to be garbage.
and that dispose method, should terminate the operations of the that object (close connections, close file, ...) and try to set the class attributes to null or dummy values.
Hope it helps you.