Dhan
Dhan

Reputation: 519

Flutter convert List<dynamic> to use in builder

There is data coming from API looks like this

"doc_list": [
 {
   "text": "test1",
   "date_added": "2022-02-20",
   "added_by_user_id": "10",
 },
 {
   "text": "test2",
   "date_added": "2022-02-21",
   "added_by_user_id": "10",
 },
]

In my Model I load this data to a dynamic list like this

List<dynamic>? org_doc_list = [];

Now when I print the data inside widget I see the data like following

[
 {
  text: test1,
  date_added: 2022-02-20,
  added_by_user_id: 10,
 },
 {
  text: test2,
  date_added: 2022-02-21,
  added_by_user_id: 10,
 }
]

All quotation marks are removed and I think it became a string. How can I convert this to use with ListView.builder() inside a widget

Thank you

Upvotes: 1

Views: 292

Answers (1)

Roman Jaquez
Roman Jaquez

Reputation: 2779

You can access it like a regular dictionary / instance of Map<String, dynamic> inside your ListView.builder, as in:


ListView.builder(
   itemCount: org_doc_list.length,
   itemBuilder: (context, index) {
       
      var org_doc = org_doc_list[index];
      
      // access properties by their key
      var text = org_doc['text'];
      var dateAdded = org_doc['date_added'];
   }
)

Upvotes: 1

Related Questions