Answers for "tostring dart"

0

tostring dart

// Works similar to Java, objects have a toString() 
// method that can be invoked, as well as overwritten.
//
// For simple types like int or double
// call .toString()


var myInt = 12;
var myIntToString = myInt.toString();

print(myIntToString);	 // prints '12'
print(myInt);         	// also prints '12', calling print on an object 
						// will implicitly call its toString() method.

// For custom objects:
// Answer provided at link from Stack Overflow:
// https://stackoverflow.com/questions/22921222/default-stringify-for-objects-equivalent-to-javas-tostring#answer-22927640

class MyClass {
    String data;

    MyClass(this.data);

    @override
    String toString() {
        return data;
    }
}

MyClass myObject = new MyClass("someData");
print(myObject); // outputs "someData", not 'Instance of MyClass'
Posted by: Guest on June-18-2021

Browse Popular Code Answers by Language