I am trying to make an app using GetX in Flutter. However, after sorting the values using the where operator in a List that has a structure of Class, I try to save it as a List and use it.
In my current code, the String displayed in the Text widget is "Instance of 'Product'". I hope you guys can tell me how to retrieve the stored value in a simple way without running a for loop.
Model and List Data are as follows.
class Product {
final String id;
final String title;
final String description;
final double price;
final String imageUrl;
bool isFavorite;
Product({
required this.id,
required this.title,
required this.description,
required this.price,
required this.imageUrl,
this.isFavorite = false,
});
}
List<Product> lodedProduct = [
Product(
id: 'p1',
title: 'Red Shirt',
description: 'A red shirt - it is pretty red!',
price: 29.99,
imageUrl:
'https://cdn.pixabay.com/photo/2016/10/02/22/17/red-t-shirt-1710578_1280.jpg',
),
Product(
id: 'p2',
title: 'Trousers',
description: 'A nice pair of trousers.',
price: 59.99,
imageUrl:
'https://upload.wikimedia.org/wikipedia/commons/thumb/e/e8/Trousers,_dress_(AM_1960.022-8).jpg/512px-Trousers,_dress_(AM_1960.022-8).jpg',
),
Product(
id: 'p3',
title: 'Yellow Scarf',
description: 'Warm and cozy - exactly what you need for the winter.',
price: 19.99,
imageUrl: 'https://live.staticflickr.com/4043/4438260868_cc79b3369d_z.jpg',
),
Product(
id: 'p4',
title: 'A Pan',
description: 'Prepare any meal you want.',
price: 49.99,
imageUrl:
'https://upload.wikimedia.org/wikipedia/commons/thumb/1/14/Cast-Iron-Pan.jpg/1024px-Cast-Iron-Pan.jpg',
),
];
The part to be displayed is as follows.
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:myshop/data/product_data.dart';
import '../models/product.dart';
class ProductDetailScreen extends StatelessWidget {
ProductDetailScreen({Key? key}) : super(key: key);
var filteredList = lodedProduct
.where((element) => element.id.contains(Get.arguments))
.map((e) => e)
.toList();
//Here, get the id from the previous page with Get.arguments. id is the 0th id in the List.
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).primaryColor,
centerTitle: true,
title: Text(Get.arguments),
),
body: Center(
child: Container(
child: Text(filteredList.toString()),
//Here, I want to get the value of filteredList as a string.
),
),
);
}
}
CodePudding user response:
Try this:
body: Center(
child: Column(
children: filteredList.map((e){
return Text(e.title);
}).toList(),
),
),
CodePudding user response:
I found a way to get the value outside the Text widget. Although it was not possible to write all the code in filteredList in one line, the Text widget made it possible to simply get the value of the List.
But I still want to get the value as filteredList.title.
Can anyone give me an answer?
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:myshop/data/product_data.dart';
import '../models/product.dart';
class ProductDetailScreen extends StatelessWidget {
ProductDetailScreen({Key? key}) : super(key: key);
var filteredList = lodedProduct
.where((element) => element.id.contains(Get.arguments))
.map((e) => e)
.toList();
late String title =
filteredList.map((e) => e.title).toList().join(",").toString();
@override
Widget build(BuildContext context) {
print(title);
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).primaryColor,
centerTitle: true,
title: Text(title.toString()),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [Text(title)],
),
),
);
}
}
