How to print only the specific parts of values from the json map?

4

I am new to flutter and I followed another post on StackOverflow about retrieving data from a json map. It worked but now I want to access only the data I need from the Map. I can't delete the excess data from the map too because I need the same map for another part of the app.

This is how the code looks like.

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return CupertinoApp(
      localizationsDelegates: <LocalizationsDelegate<dynamic>>[
        DefaultMaterialLocalizations.delegate,
        DefaultWidgetsLocalizations.delegate,
        DefaultCupertinoLocalizations.delegate,
      ],
      theme: CupertinoThemeData(
        primaryColor: Color(0xffffffff),
      ),
      home: TransactionHistoryScreen(),
    );
  }
}

class TransactionHistoryScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return CupertinoPageScaffold(
      child: Scaffold(body: Container(child: MakeList(),),),
    );
  }
}

class MakeList extends StatelessWidget {
final List<Map<String, String>> json = [
  {"branch": "B1", "xyz": "0", "ABC": "2", "MN": "2"},
  {"branch": "B2", "xyz": "0", "ABC": "0", "MN": "0"},
  {"branch": "B3", "xyz": "1", "ABC": "1", "MN": "42"},
  {"branch": "B4", "xyz": "0", "ABC": "5", "MN": "69"},
];

@override
Widget build(BuildContext context) {
  return ListView.builder(
    itemCount: json.length,
    itemBuilder: (BuildContext context, int index) {
      return ListTileClass(jsonObject: json[index]);
    },
   );
  }
}

class ListTileClass extends StatefulWidget {
final Map<String, String> jsonObject;

ListTileClass({this.jsonObject});

@override
_ListTileClassState createState() => _ListTileClassState();
}

class _ListTileClassState extends State<ListTileClass> {
  @override
  Widget build(BuildContext context) {
    return ListTile(
      title: Container(
        child: Row(
          children: makeWidgetChildren(widget.jsonObject),
        ),
      ),
    );
  }

  List<Widget> makeWidgetChildren(jsonObject) {
    List<Widget> children = [];
    jsonObject.keys.forEach(
      (key) => {
        children.add(
          Padding(
              child: Text('${jsonObject[key]}'), padding: EdgeInsets.all(8.0)),
        ),
      },
    );
    return children;
  }
}

Using this code I can print the value of each key of the map. How can I access specific keys? As an example, how can I print just the "branch" and "xyz" values?

android
flutter
dart
asked on Stack Overflow Jan 8, 2021 by Haritha Madhushanka • edited Jan 8, 2021 by a_local_nobody

1 Answer

2

You can use forEach in dart, in many cases:

void main() {
  final List<Map<String, String>> json = [
  {"branch": "B1", "xyz": "0", "ABC": "2", "MN": "2"},
  {"branch": "B2", "xyz": "0", "ABC": "0", "MN": "0"},
  {"branch": "B3", "xyz": "1", "ABC": "1", "MN": "42"},
  {"branch": "B4", "xyz": "0", "ABC": "5", "MN": "69"},
];
  json.forEach((item) => print("Branch : ${item['branch']}, xyz : ${item['xyz']}"));
}

output :

Branch : B1, xyz : 0
Branch : B2, xyz : 0
Branch : B3, xyz : 1
Branch : B4, xyz : 0
answered on Stack Overflow Jan 8, 2021 by AVEbrahimi

User contributions licensed under CC BY-SA 3.0