PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, July 23, 2022

[FIXED] How to get another key value from an array of json?

 July 23, 2022     dart, flutter, json     No comments   

Issue

Lets say I have these arrayed JSON values

[{operation_id: 2, operation_name: FAITHFUL BELIEVERS}, 
{operation_id: 3, operation_name: SAMPLE OP}, 
{operation_id: 4, operation_name: SAMPLE OP 2}]

Now I will select the operation name 'SAMPLE OP' but I want to display the value of its operation_id. How would I do that?


Solution

Your JSON is a list of maps, so use where on the list to filter it by your predicate. Better still, use firstWhere as we assume there's just one match.

The match function returns true if the operation name member of the map matches.

firstWhere returns the first matching map, and you want the operation id member of that map.

  final id = list
      .firstWhere((m) => m['operation_name'] == 'SAMPLE OP')['operation_id'];


Answered By - Richard Heap
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing