How to print value of an enum? [duplicate] - dart

Before enums were available in Dart I wrote some cumbersome and hard to maintain code to simulate enums and now want to simplify it. I need to get the name of the enum as a string such as can be done with Java but cannot.
For instance little test code snippet returns 'day.MONDAY' in each case when what I want is 'MONDAY"
enum day {MONDAY, TUESDAY}
print( 'Today is $day.MONDAY');
print( 'Today is $day.MONDAY.toString()');
Am I correct that to get just 'MONDAY' I will need to parse the string?

Dart 2.7 comes with new feature called Extension methods. Now you can write your own methods for Enum as simple as that!
enum Day { monday, tuesday }
extension ParseToString on Day {
String toShortString() {
return this.toString().split('.').last;
}
}
main() {
Day monday = Day.monday;
print(monday.toShortString()); //prints 'monday'
}

Bit shorter:
String day = theDay.toString().split('.').last;

Update Dart 2.15:
enum Day {
monday,
tuesday,
}
You can use name property on the enum.
String monday = Day.monday.name; // 'monday'
Old solution:
1. Direct way:
var dayInString = describeEnum(Day.monday);
print(dayInString); // prints 'monday'
2. Using Extension:
extension DayEx on Day {
String get name => describeEnum(this);
}
You can use it like:
void main() {
Day monday = Day.monday;
print(monday.name); // 'monday'
}

It used to be correct that the only way to get the source name
of the enum value was through the toString method which returns "day.MONDAY", and not the more useful "MONDAY".
Since Dart 2.15, enums have exposed a extension getter which returns just the source name, so day.MONDAY.name == "MONDAY".
Since Dart 2.17, you can also add your own members to enum declarations, and choose to provide another name for
a value than justits source name, e.g., with a more appropriate capitalization:
enum Day {
monday("Monday"),
tuesday("Tuesday"),
// ...
saturday("Saturday");
sunday("Sunday");
final String name;
Day(this.name);
// And other members too.
bool get isWorkday => index < saturday.index;
}
Then you get Day.sunday.name == "Sunday" (hiding the extension name getter which would return "sunday").
Before these features, you could only get the name from the toString string, extracting the rest of the string as:
day theDay = day.MONDAY;
print(theDay.toString().substring(theDay.toString().indexOf('.') + 1));
which was admittedly hardly convenient.
Another way to get the enum name as a string, one which is shorter, but also less efficient because it creates an unnecessary string for first part of the string too, was:
theDay.toString().split('.').last
If performance doesn't matter, that's probably what I'd write, just for brevity.
If you want to iterate all the values, you can do it using day.values:
for (day theDay in day.values) {
print(theDay);
}

Simplest way to get the name of an enum is a standard method from the flutter/foundation.dart
describeEnum(enumObject)
enum Day {
monday, tuesday, wednesday, thursday, friday, saturday, sunday
}
void validateDescribeEnum() {
assert(Day.monday.toString() == 'Day.monday');
assert(describeEnum(Day.monday) == 'monday');
}

enum day {MONDAY, TUESDAY}
print( 'Today is ${describeEnum(day.MONDAY)}' );
console output: Today is MONDAY

There is a more elegant solution:
enum SomeStatus {
element1,
element2,
element3,
element4,
}
const Map<SomeStatus, String> SomeStatusName = {
SomeStatus.element1: "Element 1",
SomeStatus.element2: "Element 2",
SomeStatus.element3: "Element 3",
SomeStatus.element4: "Element 4",
};
print(SomeStatusName[SomeStatus.element2]) // prints: "Element 2"

Sometimes I need to separate ui-value and real-value, so I defined keys and values using Map. This way, we have more flexiblity. And by using extension (since Dart 2.7), I made a method to read its key and value.
enum Status {
progess,
done,
}
extension StatusExt on Status {
static const Map<Status, String> keys = {
Status.progess: 'progess',
Status.done: 'done',
};
static const Map<Status, String> values = {
Status.progess: 'In Progress',
Status.done: 'Well done',
};
String get key => keys[this];
String get value => values[this];
// NEW
static Status fromRaw(String raw) => keys.entries
.firstWhere((e) => e.value == raw, orElse: () => null)
?.key;
}
// usage 1
Status status = Status.done;
String statusKey = status.key; // done
String statusValue = status.value; // Well done
// usage 2 (easy to make key and value list)
List<Status> statuses = Status.values;
List<String> statusKeys = statuses.map((e) => e.key).toList();
List<String> statusValues = statuses.map((e) => e.value).toList();
// usage 3. create Status enum from string.
Status done1 = StatusExt.fromRaw('done') // Status.done
Status done2 = StatusExt.fromRaw('dude') // null

With Dart 2.17 we now have general support for members on enums. That means we can add fields holding state, constructors that set that state, methods with functionality, and even override existing members.
Example:
enum Day {
MONDAY("Monday"),
TUESDAY("Tuesday");
const Day(this.text);
final String text;
}
Output:
void main() {
const day = Day.MONDAY;
print(day.text); /// Monday
}
For above functionality override dart version like below which target 2.17 and greater
environment:
sdk: ">=2.17.0 <3.0.0"

I got so over this I made a package:
https://pub.dev/packages/enum_to_string
Also has a handy function that takes enum.ValueOne and parses it to "Value one"
Its a simple little library but its unit tested and I welcome any additions for edge cases.

I use the functions below to get the name of the enum value and, vise versa, the enum value by the name:
String enumValueToString(Object o) => o.toString().split('.').last;
T enumValueFromString<T>(String key, Iterable<T> values) => values.firstWhere(
(v) => v != null && key == enumValueToString(v),
orElse: () => null,
);
When using Dart 2.7 and newer, extension methods would work here (as well as for any other Objects):
extension EnumX on Object {
String asString() => toString().split('.').last;
}
The implementation above is not dependant on the specific enums.
Usage examples:
enum Fruits {avocado, banana, orange}
...
final banana = enumValueFromString('banana', Fruits.values);
print(enumValueToString(banana)); // prints: "banana"
print(banana.asString()); // prints: "banana"
Edit from 2020-04-05: Added nullability checks. values parameter could be Iterable, not necessarily List. Added extensions method implementation. Removed <Fruits> annotation from the example to show that the class name duplication is not required.

I use structure like below:
abstract class Strings {
static const angry = "Dammit!";
static const happy = "Yay!";
static const sad = "QQ";
}

Dart 2.15 includes an extension to make this easy:
enum day {MONDAY, TUESDAY}
print( 'Today is ${day.MONDAY.name}');
Until the changes in https://github.com/dart-lang/sdk/commit/18f37dd8f3db6486f785b2c42a48dfa82de0948b are rolled out to a stable version of Dart, the other clever but more complex answers here are very useful.

One more way:
enum Length {
TEN,
TWENTY,
THIRTY,
NONE,
}
extension LengthValue on Length {
static const _values = [10, 20, 30, 0];
int get value => _values[this.index];
}

since dart 2.15 just use ".name"
enum day {monday, tuesday}
print( 'Today is ${day.monday.name}');

My approach is not fundamentally different, but might be slightly more convenient in some cases:
enum Day {
monday,
tuesday,
}
String dayToString(Day d) {
return '$d'.split('.').last;
}
In Dart, you cannot customize an enum's toString method, so I think this helper function workaround is necessary and it's one of the best approaches. If you wanted to be more correct in this case, you could make the first letter of the returned string uppercase.
You could also add a dayFromString function
Day dayFromString(String s) {
return Day.values.firstWhere((v) => dayToString(v) == s);
}
Example usage:
void main() {
Day today = Day.monday;
print('Today is: ${dayToString(today)}');
Day tomorrow = dayFromString("tuesday");
print(tomorrow is Day);
}

enum day {MONDAY, TUESDAY}
print(day.toString().split('.')[1]);
OR
print(day.toString().split('.').last);

Create a class to help:
class Enum {
Enum._();
static String name(value) {
return value.toString().split('.').last;
}
}
and call:
Enum.name(myEnumValue);

One of the good ways I found in the answer is
String day = theDay.toString().split('.').last;
But I would not suggest doing this as dart provide us a better way.
Define an extension for the enum, may be in the same file as:
enum Day {
monday, tuesday, wednesday, thursday, friday, saturday, sunday
}
extension DayExtension on Day {
String get value => describeEnum(this);
}
You need to do import 'package:flutter/foundation.dart'; for this.

I had the same problem in one of my projects and existing solutions were not very clean and it didn't support advanced features like json serialization/deserialization.
Flutter natively doesn't currently support enum with values, however, I managed to develop a helper package Vnum using class and reflectors implementation to overcome this issue.
Address to the repository:
https://github.com/AmirKamali/Flutter_Vnum
To answer your problem using Vnum, you could implement your code as below:
#VnumDefinition
class Visibility extends Vnum<String> {
static const VISIBLE = const Visibility.define("VISIBLE");
static const COLLAPSED = const Visibility.define("COLLAPSED");
static const HIDDEN = const Visibility.define("HIDDEN");
const Visibility.define(String fromValue) : super.define(fromValue);
factory Visibility(String value) => Vnum.fromValue(value,Visibility);
}
You can use it like :
var visibility = Visibility('COLLAPSED');
print(visibility.value);
There's more documentation in the github repo, hope it helps you out.

Instead of defining extension for every enum, we can define extension on object and get access to .enumValue from any enum.
void main() {
// ❌ Without Extension ❌
print(Countries.Cote_d_Ivoire.toString().split('.').last.replaceAll("_", " ")); // Cote d Ivoire
print(Movies.Romance.toString().split('.').last.replaceAll("_", " ")); //Romance
// ✅ With Extension ✅
print(Countries.Cote_d_Ivoire.enumValue); // Cote d Ivoire
print(Movies.Romance.enumValue); //Romance
}
enum Countries { United_States, United_Kingdom, Germany, Japan, Cote_d_Ivoire }
enum Movies { Romance, Science_Fiction, Romantic_Comedy, Martial_arts }
extension PrettyEnum on Object {
String get enumValue => this.toString().split('.').last.replaceAll("_", " ");
}
With this, you can even define multi-word enum where words are separated by _(underscore) in its name.

As from Dart 2.15, you can get enum value from
print(MyEnum.one.name);
// and for getting enum from value you use
print(MyEnum.values.byName('two');

try this solution:
extension EnumValueToString on Enum {
String valueAsString() {
return describeEnum(this);
}
}
how to used it:
enum.valueAsString()

dart 2.15 is now supporting this
you can just type
print(day.MONDAY.name); //gives you: MONDAY

Since Dart 2.15, we can just do Day.monday.name, where
enum Day { monday, tuesday }

As from Dart version 2.15, you can access the String value of an enum constant using .name:
enum day {MONDAY, TUESDAY}
void main() {
print('Today is ${day.MONDAY.name}');
// Outputs: Today is MONDAY
}
You can read in detail about all the enum improvements in the official Dart 2.15 release blog post.

as of dart 2.15 you can use .name to get the name of enum elements.
enum day {MONDAY, TUESDAY}
print(day.MONDAY.name); // prints MONDAY

Dart version 2.15 has introduced name property on enums.
Example
void main() {
MyEnum.values.forEach((e) => print(e.name));
}
enum MyEnum { value1, Value2, VALUE2 }
Output:
value1
Value2
VALUE2

For those who require enum with values use this approach as Dart doesn't support this:
class MyEnumClass {
static String get KEY_1 => 'value 1';
static String get KEY_2 => 'value 2';
static String get KEY_3 => 'value 3';
...
}
// Usage:
print(MyEnumClass.KEY_1); // value 1
print(MyEnumClass.KEY_2); // value 2
print(MyEnumClass.KEY_3); // value 3
...
And sure you may put whatever types you need.

now with null safety it looks like this
String enumToString(Object? o) => o != null ? o.toString().split('.').last : '';
T? enumFromString<T>(String key, List<T> values) {
try {
return values.firstWhere((v) => key == enumToString(v));
} catch(e) {
return null;
}
}

Related

How to handle future enum values in Dart 2.17+

My app is connected to a custom made peripheral and draws some values from it as ints which I want to convert to enums to streamline the app code.
As the hardware evolves overtime, it is of known fact that new values will come.
In my Flutter app, I handle known values (by the time of app writing) through enum extensions, providing an 'unknown' value as follows:
// Battery live data originator protocol
enum BatteryLiveDataProtocol {
unknown, // 0
rs485, // 1
ttl, // 2
canbus, // 3
uart, //4
;
}
extension BatteryLiveDataProtocolExtension on BatteryLiveDataProtocol {
/// Build the enum out of the int number
static BatteryLiveDataProtocol fromNumber(int number) {
switch (number) {
case 1:
return ttl;
case 2:
return rs485;
case 3:
return canbus;
case 0:
default:
return unknown;
}
}
/// Returns the number correpsonding to the enum
int get number {
switch (this) {
case rs485:
return 1;
case ttl:
return 2;
case canbus:
return 3;
case unknown:
default:
return 0;
}
}
As you can see, this code support future version in a quite handily:
const futureValue = 13;
final p = BatteryLiveDataProtocol.fromNumber(futureValue);
Since the introduction of Dart 2.17 (& Flutter 3) I'd like to take advantage of the enhanced enums as follows:
// Battery live data originato protocol
enum BatteryLiveDataProtocol {
unknown(),
rs485(1),
ttl(2),
canbus(3),
uart(4),
;
const BatteryLiveDataProtocol(this.value);
final num value;
}
But then how can I handle future enum values?
I could do that, everywhere I want to convert the int to the enum:
BatteryLiveDataProtocol p;
try {
const futureValue = 13;
p = BatteryLiveDataProtocol(futureValue);
} catch(e) {
p = BatteryLiveDataProtocol.unknown;
}
But this is cumbersome, isn't it? It'll be cool to have the possibility to provide the body of const BatteryLiveDataProtocol(this.value); ourselves.
[UPDATE] Btw, not sure it was clear enough, but it is still possible to create a static function within the enhanced enum as follows:
static BatteryLiveDataProtocol fromValue(int v) {
BatteryLiveDataProtocol p;
try {
p = BatteryLiveDataProtocol[v];
} catch (e) {
p = unknown;
}
return p;
}
And use it as follows:
const futureValue = 13;
final p = BatteryLiveDataProtocol.fromValue(futureValue);
// Makes p being set to "unknown".
So my question #1: is there a better/clever (aka handy) way to handle future values?
Question #2: Others language handle future enum (in Swift for example): is there a way to generate unknown(actualValue) as a dynamic enum value?
So my question #1: is there a better/clever (aka handy) way to handle future values?
I think you can improve your enum by moving the definition of fromNumber and number into the enum itself rather than as an extension.
Additionally I think you can improve the implementations of both fromNumber and number.
For number, enums already have a getter called index which returns the same value as what you want, so you can simply return the index in your number method.
For fromNumber we can turn it into a factory constructor, and since all enums have a static values list (that contains all enum values in the specific order they are defined), we can get the value we want by indexing into the values list. And for any invalid index we would return unknown.
The benefit of these changes to number and fromNumber is that you will no longer need to update these methods when you add another enum value.
The updated enum is as follows:
enum BatteryLiveDataProtocol {
unknown, // 0
rs485, // 1
ttl, // 2
canbus, // 3
uart, //4
;
factory BatteryLiveDataProtocol.fromNumber(int number) =>
number >= values.length || number < 0 ? unknown : values[number];
int get number => index;
}
Question #2: Others language handle future enum (in Swift for example): is there a way to generate unknown(actualValue) as a dynamic enum value?
No, dart's enhanced enums do not work like swift enums, enums in dart are closer to java enums. Swift calls this feature associated values, but dart enums do not have this feature.
Thanks to #mmcdon20 for the factory proposal (and btw, I couldn't replace static by factory in the code below).
But one want to make sure one handle any values: values and index may be different as in the modified example I propose below where the value of unknown is now set to -1 (index is 0).
So the code has to take care of it:
/// Battery live data originato protocol
enum BatteryLiveDataProtocol {
unknown(-1),
rs485(1),
ttl(2),
canbus(3),
uart(4),
;
const BatteryLiveDataProtocol(this.value);
final num value;
factory BatteryLiveDataProtocol.fromValue(int v) =>
values.firstWhere((x) => x.value == v, orElse: () => unknown);
}

How to set value inside nested forEach() in java8?

I have a case in which I am iterating the List<DiscountClass> and need to compare the list value with another List<TypeCode>, based on satisfying the condition (when Discount.code equals TypeCode.code) I need to set Discount.setCodeDescr(). How to achieve this with nested forEach loop in java 8? (I am not able to set after comparing the values in java 8 forEach).
for (Discount dis : discountList) {
for (TypeCode code : typeCodeList) {
if (dis.getCode().equals(code.getCode())) {
dis.setCodeDesc(code.getCodeDesc());
}
}
}
A possible solution using java 8 lambdas could look like this:
discountList.forEach(dis -> {
typeCodeList
.stream()
.filter(code -> dis.getCode().equals(code.getCode()))
.findAny()
.ifPresent(code -> dis.setCodeDesc(code.getCodeDesc()));
});
For each discount you filter the TypeCodes according to the code and if you find any you set the desc poperty to the one of the found TypeCode.
The other answer showed how to convert a nested loop to a nested functional loop.
But instead of iterating over a list of TypeCode, it's better to use a HashMap to get random access, or an enum like this:
public enum TypeCode {
CODE_1("description of code 1"),
CODE_2("description of code 2");
private String desc;
TypeCode(String desc) {
this.desc = desc;
}
public String getDesc() {
return desc;
}
}
public class Discount {
private String typeCode; //assuming you can't have the type as TypeCode
private String desc;
public Discount(String typeCode) {
this.typeCode = typeCode;
}
//getters/setters
}
Then your code will change to:
Discount d1 = new Discount("CODE_1");
Discount d2 = new Discount("CODE_2");
List<Discount> discounts = List.of(d1, d2);
discounts.forEach(discount ->
discount.setDesc(TypeCode.valueOf(discount.getTypeCode()).getDesc()));

How to check if value is enum?

I would like to check if a value in dart is an Enum, and if so, serialize it automatically.
My sticking point is how to test if a value is an enum.
This is how I am testing for other types
if (T == int)
val = prefs.getInt(this._key);
else if (T == double)
val = prefs.getDouble(this._key);
else if (T == bool)
val = prefs.getBool(this._key);
else if (T == String)
val = prefs.getString(this._key);
else if ([""] is T)
val = prefs.getStringList(this._key);
but I cannot seem to compare T to an enum
Since all enums have the same methods I was hoping to sense them and handle them accordingly
As of late 2021, all enums extend Enum
You can now finally use is Enum directly:
enum MyEnum {one, two, three}
var x = "a";
var y = MyEnum.one;
print(x is Enum); // false
print(y is Enum); // true
Which also means you can now create extensions for enums:
extension EnumExtension on Enum { ... }
If you need to check is dynamic value enum type. You can use the next approach. The main idea is quite similar to #Oniya Daniel's answer.
enum Fruit {
banana,
apple,
orange,
}
Next method to check isEnum condition
bool isEnum(dynamic data) {
final split = data.toString().split('.');
return split.length > 1 && split[0] == data.runtimeType.toString();
}
Test result below
test('check enum runtime', () {
expect(isEnum(Fruit.banana), true);
expect(isEnum(null), false);
expect(isEnum(''), false);
expect(isEnum('banana'), false);
});
P.S.: to get enum String value good to use describeEnum(<enum_value>) from the package:flutter/foundation.dart
I don't have enough reputation to comment, or I would have added a statement to the answer by #Dmitry Puzak. In Dmitry's method, if data is passed as the String "String.3.0.1", for instance, the method will incorrectly identify the parameter as an enum. The flutter-provided method, describeEnum, attempts to check that it's parameter is an enum and throws an exception if it's not, but in my experience the exception isn't always thrown when the parameter to describeEnum is a String value containing dots.
For a project I'm working, I modified Dmitry's isEnum method to the following, adding the first if block inside the method:
bool isEnum(dynamic data) {
if (data.runtimeType == "".runtimeType) {
return false;
}
final split = data.toString().split('.');
return split.length > 1 && split[0] == data.runtimeType.toString();
}
This makes sure to return false for any String parameter.
I can't offhand think of any other instance where the runtime type of a variable could possibly match the first part of the split done on the toString() return value.
Convert the enum to a list and check if the list contains the value you intend to check.
enum ObjectType {
TypeA, TypeB
}
// checking against enum values
print(ObjectType.values.contains(ObjectType.TypeA)); // true
print(ObjectType.values.contains(ObjectType.TypeB)); // true
// checking against string value
print(ObjectType.values.contains("ObjectType.TypeA")); // false
// checking against boolean
print(ObjectType.values.contains(false)); // false
// checking against int
print(ObjectType.values.contains(234)); // false
// checking against double
print(ObjectType.values.contains(2.345)); // false

Use getters and setters to create string from 1-7 integers

I'm trying to take a 1-7 integer value and print out a day for each value with an enum.
I get an error in the class mapping from firestore, "isn't a field in the enclosing class"
So if 1 is passed in then "Monday" is given
if 2 is passed in then "Tuesday" is given
enum _Days {
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
}
class HeadingItem implements ListItem {
String _weekday;
final int time;
final DocumentReference reference;
set day(int weekday) {
var value = _Days.values[weekday - 1].toString();
var idx = value.indexOf(".") + 1;
var result = value.substring(idx, value.length);
_weekday = result;
}
String get day {
return _weekday;
}
HeadingItem.fromMap(Map<String, dynamic> map, {this.reference})
: assert(map['day'] != null),
assert(map['time'] != null),
day = map['day'], // 'day' isn't a field in the enclosing class <--- this is the error that im stuck on...
time = map['time'];
HeadingItem.fromSnapshot(DocumentSnapshot snapshot) : this.fromMap(snapshot.data, reference: snapshot.reference);
}
Change
String get day {
return _weekday;
}
to this
String day = _weekday;
Notice that in your code, day is technically a set of methods of this that allows us to work with day as if it were a member of this; day is not actually a member.
Thus, in order for your initializer list to work as you intended it to, it would already need to have access to this in order to provide the functionality you want (for example, to set the value of the internal member _weekday).
However, as mentioned in the Language Tour of Dart in the section on Initializer Lists, the initializer list does not have access to this. Rather we should see the initializer list as values to assign to members of this during the instantiation.

Dart: How to bind to variables annotated with int via Web UI?

What is the best practice in Dart when dealing with classes as data records?
To Elaborate: When writing an app, it is likely that a class for a table row will be created. As in
class Item { int itemid, String itemName, double score }
Item item = new Item();
This allows compile time catching of any typos etc. in Dart. (Unlike using a class that relies on NoSuchMethod.)
It will also need a corresponding string structure to bind to the HTML such as
<input id="itemname" type="text" bind-value="itemEdit.itemName">
So the Dart would be:
class ItemEdit { String itemId, String itemName, String score }
ItemEdit itemEdit = new ItemEdit();
Next we need a way to get from one to the other, so we add a method to Item
fromStrings(ItemEdit ie) {
itemid = ie.itemId == null ? null : int.parse(ie.itemId);
itemName = ie.ItemName;
score = ie.score == null ? null : double.parse(ie.score);
}
And the other way around:
toStrings(ItemEdit ie) {
ie.itemid = itemId == null ? '' : ie.itemId.toString();
ie. itemName = itemName == null ? '' : itemname; // Web_ui objects to nulls
ie.score = score == null ? null : score.toStringAsFixed(2);
}
Also, we get jason data from a database, so we need to add another method to Item:
fromJson(final String j) {
Map v = JSON.parse(j);
itemid = v['itemid'];
itemname = v['itemname'];
score = v['score'];
}
And we need to be able to revert to default values:
setDefaults() { itemId = 0; itemName = "New item"; score = 0; }
This verbosity gets me feeling like I am writing COBOL again!
There is something fundamental missing here - either in my understanding, or in the Dart/WebUI libraries.
What I would like to write is something like
class Item extends DataRecord {
int itemid = 0,
String itemName = 'New item',
double score = 0.0;
}
Then, without further coding, to be able to write code such as
item.toStrings();
...
item.fromStrings();
...
item.fromJson(json);
...
item.setDefaults(); // results in {0,'New item',0.0}
And to be able to write in the HTML:
value="{{item.strings.score}}"
If this was possible, it would be quicker, simpler, clearer, and less error prone than the code I am writing at the moment.
(Full disclosure, this answer is written with the assumption that at least one bug will be fixed. See below)
Three suggestions that might help.
Use named constructors to parse and create objects.
Take advantage of toJson() when encoding to JSON.
Use bind-value-as-number from Web UI
1) Named constructors
import 'dart:json' as json;
class Item {
int itemid;
String itemName;
double score;
Item.fromJson(String json) {
Map data = json.parse(json);
itemid = data['itemid'];
itemName = data['itemName'];
score = data['score'];
}
}
2) Encoding to JSON
The dart:json library has a stringify function to turn an object into a JSON string. If the algorithm encounters an object that is not a string, null, number, boolean, or collection of those, it will call toJson() on that object and expect something that is JSON-encodable.
class Item {
int itemid;
String itemName;
double score;
Map toJson() {
return {'itemid':itemid, 'itemName':itemName, 'score':score};
}
}
3) Now, having said that, sounds like you want to easily bind to HTML fields and get primitive values back, not just strings. Try this:
<input type="number" min="1" bind-value-as-number="myInt" />
(Note, there seems to be a bug with this functionality. See https://github.com/dart-lang/web-ui/issues/317)
(from https://groups.google.com/a/dartlang.org/forum/#!topic/web-ui/8JEAA5OxJOc)
Just found a way to perhaps help a little in the this situation:
class obj {
int gapX;
String get gapXStr => gapX.toString();
set gapXStr(String s) => gapX = int.Parse(s);
...
Now, from the HTML you can use, for example
bind-value="gapXStr"
and in code you can use
x += ob.gapX;

Resources