I have and object and I wondering if there is a simple way to iterate through it keys and values?
class Post {
String id;
String title;
String article;
Post(
{
this.id,
this.title,
this.article
}
);
}
Add the http package. Make a network request using the http package. Convert the response into a custom Dart object. Fetch and display the data with Flutter.
There is not.
What you are asking for is reflection. You can use the dart:mirrors
API for that, if the library is available on your platform (it's not in Flutter).
You might be able to generate code for it by using package:reflectable
.
Or you can use package:json_serializable
to generate a Map
from an object.
If you are just trying to do something for one particular class, I'd just manually
write:
dart
Map<String, dynamic> toJson() => {"id": id, "title": title, "article": article};
and then use that when you want to iterate.
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