coolhack7
coolhack7

Reputation: 1234

Extract Data from dynamic Map

I have a Map<String, Products> from which I would like to fetch all the data that gets stored. Going by my amateur like assessment of the code I've written, I believe I have managed to store the data inside the map but the problem arises when I try and print the same. Everytime I try doing so, the output that gets thrown at my face says id Instance of 'Products'. I would like to know what I can do to parse this output so that I get to see all the data in the key value form. The output I desire should look like,

id : abc
title : Title
description : This is a Shirt
price : 29.99

Here is the code

class Products {
  final String id;
  final String title;
  final String description;
  final double price;

  Products({
    required this.id,
    required this.title,
    required this.description,
    required this.price
  });
}

class ProductItems {
  Map<String, Products> _items = {};

  Map<String, Products> get items {
    return {..._items};
  }

  void addItems(Products products) {
    _items.putIfAbsent(products.id, () => Products(
        id: products.id,
        title: products.title,
        description: products.description,
        price: products.price
      )
    );
  }
  void printItems() {
    items.forEach((key, value) {
      print('${key} ${value}');
    });
  }
}

void main() {
  ProductItems productItems = ProductItems();
  Products products = new Products(
      id: "id",
      title: "title",
      description: "This is a Red Shirt",
      price: 29.99
  );
  productItems.addItems(products);
  productItems.printItems();
}

Upvotes: 0

Views: 327

Answers (1)

Jaison Thomas
Jaison Thomas

Reputation: 403

First of to print the instance of a class you would need to define a toString function in the class as shown below

class Products {
  final String id;
  final String title;
  final String description;
  final double price;

  Products({
    required this.id,
    required this.title,
    required this.description,
    required this.price
  });
  
  //this is the mentioned toString Function 
  String toString() {
    return "id: " + id + ", title: " + title + ", description: " 
      + description + ", price: " + price.toString();
  }
  
}

Secondly a minor edit to your print items function so that it prints the value of your class

class ProductItems {
  Map<String, Products> _items = {};

  Map<String, Products> get items {
    return {..._items};
  }

  void addItems(Products products) {
    _items.putIfAbsent(products.id, () => Products(
        id: products.id,
        title: products.title,
        description: products.description,
        price: products.price
      )
    );
  }
  void printItems() {
    items.forEach((key, value) {
      //edits done here 
      print("Key: " + key);
      print("Value: " + value.toString());
    });
  }
}

And finally to test it out I edited your main function to look like

void main() {
  ProductItems productItems = ProductItems();
  
  Products products = new Products(
      id: "12",
      title: "title",
      description: "This is a Red Shirt",
      price: 29.99
  );
  productItems.addItems(products);
  
  products = new Products(
      id: "123",
      title: "Im new",
      description: "I'm a contribution by jaison",
      price: 99.99
  );
  productItems.addItems(products);
  
  productItems.printItems();
}

I've added 2 product class items so that the output is clearer. and will look like

Key: 12

Value: id: 12, title: title, description: This is a Red Shirt, price: 29.99

Key: 123

Value: id: 123, title: Im new, description: I'm a contribution by jaison, price: 99.99

You almost got it, it was just the printing of a class instance.

Upvotes: 1

Related Questions