I have just take a look at the 3º tutorial from dart, creating the rating component. I was wondering if there is same method which is called when stringifying an object, something similar to Java's toString.
For example:
MyClass myObject = new MyClass();
System.out.println(myObject);
Will call MyClass.toString() if overwriten, else will call it's parent until java.lang.Object is reached giving a default toString.
I find kind ugly (completely subjective) doing:
<span ng-repeat="star in cmp.stars" > {{star.toString()}} </span>
I would rather do:
<span ng-repeat="star in cmp.stars" > {{star}} </span>
And give the implementation of how I want it to display at an averwritten method. Is this possible?
By default the toString() method will return a string that lists the name of the class followed by an @ sign and then a hexadecimal representation of the memory location the instantiated object has been assigned to.
toString() method returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read.
Definition and Usage. The toString() method returns a string as a string. The toString() method does not change the original string. The toString() method can be used to convert a string object into a string.
Java - toString() Method The method is used to get a String object representing the value of the Number Object. If the method takes a primitive data type as an argument, then the String object representing the primitive data type value is returned.
If you have something like this:
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'
I think this might be what you are looking for.
Yes it works like this for print
, String interpolation or Angular mustaches.
By overriding the String toString()
method on your object the displayed value will be the result of this toString()
call. If there's no toString()
defined in the class hierarchy the toString()
of Object
will be called (which will return Instance of 'MyClass'
for class MyClass{}
).
You may be interesting look how Rating component was implemented in Angular Dart UI project. Check this out.
Sergey.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With