Objects in dart
You,•dart + flutter
There are dart objects and JS style "OBJECTS"
- The JS style objects are not true objects, but some kind of hash tables.
- Class instances are true objects.
Here is some code to help clarify:
class Student
{
String stname = "";
int stid = 0;
Student(this.stname, this.stid); //shortcut constructor
getInfo()
{
return "Student: name : $stname id: $stid";
}
}
class Momo
{
String covering = "maida";
String stuffing = "cabbage";
Momo(String outerlayer, String inside)
{
covering = outerlayer;
stuffing = inside;
}
getInfo()
{
return "This Momo has $stuffing covered by $covering";
}
}
void main()
{
Student c = Student('Zoomer',42);
print("Type of c is: ${c.runtimeType}, it contains ${c.getInfo()}");
Student d = Student ("zonto", 22);
print("Type of d is: ${d.runtimeType}, it contains ${d.getInfo()}");
Momo m = Momo("atta","potato");
print("Type of m is: ${m.runtimeType}, it contains ${m.getInfo()}");
//in dart, this is not a class, it is a "Hashmap"
var x = {"stname":"SuperBoy","stid":22};
print("Type of x is: ${x.runtimeType}, it contains $x");
}
Output:
Type of c is: Student, it contains Student: name : Zoomer id: 42
Type of d is: Student, it contains Student: name : zonto id: 22
Type of m is: Momo, it contains This Momo has potato covered by atta
Type of x is: JsLinkedHashMap<String, Object>, it contains {stname: SuperBoy, stid: 22}