Flutter to string function

Viewed 312

can you help me please. I'm new in Flutter and i try to use share plugin. That works well. I created a list of products in my app.

Share.share(widget.selectedList.productsAndStatus.toString());

And when i copy that, it shows like this:

[{productName: Dawd, productDescription: , productStatus: false}, {productName: Skap, productDescription: , productStatus: false}]

Is there any way to make it copy only name of product and description of product without '[, ], {, }, productName, productDescription, productStatus' ?

1 Answers

Do something like below.

class Product {
  static int counter = 0;
  final int _id = ++counter;
  final String? name;
  Product({this.name});
  @override
  String toString() => '$name with id $_id';
}

void main() {
  Product p1 = Product(name: 'Computer');
  Product p2 = Product(name: 'Refrigerator');
  print(p1.toString());
  print(p2.toString()); 
}

Output:

Computer with id 1
Refrigerator with id 2
Related