Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter - Iterate through object keys and values

Tags:

flutter

dart

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
      }
      );
}
like image 628
Zuhair Ali Avatar asked Dec 05 '18 10:12

Zuhair Ali


People also ask

How do you get data from an object in flutter?

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.


1 Answers

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.

like image 165
lrn Avatar answered Oct 08 '22 02:10

lrn