我很难理解如何使用提供者通过ID过滤数据。我尝试过的方法产生了以下错误:
Class '_InternalLinkedHashMap<String, Object>' has no instance getter 'id'.
Receiver: _LinkedHashMap len:8
Tried calling: id鉴于我在颤振方面的一点经验,任何帮助或建议都将受到高度赞赏。我所写的代码如下:
class PopularDishesProvider with ChangeNotifier {
final Map<String, dynamic> _popularDishes = { //This is the JSON map that I would like to filter from
"data": [
{
"id": "1",
"name": "Chicken Tandoor Combo",
"restaurantName": "Tandoori House",
"price": "455",
"rating": "4.3",
"totalRatings": "154",
"image": "assets/images/Rectangle 17323.png",
"isFavourite": false
},
{
"id": "2",
"name": "Pan Cake",
"restaurantName": "The Pancake Centre",
"price": "250",
"rating": "4.7",
"totalRatings": "256",
"image": "assets/images/Rectangle 17324.png",
"isFavourite": false
},
{
"id": "3",
"name": "Salad",
"restaurantName": "The Pancake House",
"price": "180",
"rating": "4.1",
"totalRatings": "203",
"image": "assets/images/Rectangle 17325.png",
"isFavourite": false
},
{
"id": "4",
"name": "Roast Chicken",
"restaurantName": "Kentucky\"s Fried Chicken",
"price": "550",
"rating": "4.8",
"totalRatings": "1000",
"image": "assets/images/Rectangle 17323 (2).png",
"isFavourite": false
},
{
"id": "5",
"name": "Ice Cream",
"restaurantName": "Naturals",
"price": "80",
"rating": "5.0",
"totalRatings": "1500",
"image": "assets/images/Rectangle 17324 (2).png",
"isFavourite": false
},
{
"id": "6",
"name": "Chicken Tandoor Combo",
"restaurantName": "Tandoori House",
"price": "455",
"rating": "4.3",
"totalRatings": "154",
"image": "assets/images/Rectangle 17323.png",
"isFavourite": false
},
{
"id": "7",
"name": "Pan Cake",
"restaurantName": "The Pancake Centre",
"price": "250",
"rating": "4.7",
"totalRatings": "256",
"image": "assets/images/Rectangle 17324.png",
"isFavourite": false
},
{
"id": "8",
"name": "Salad",
"restaurantName": "The Pancake House",
"price": "180",
"rating": "4.1",
"totalRatings": "203",
"image": "assets/images/Rectangle 17325.png",
"isFavourite": false
},
{
"id": "9",
"name": "Roast Chicken",
"restaurantName": "Kentucky\"s Fried Chicken",
"price": "550",
"rating": "4.8",
"totalRatings": "1000",
"image": "assets/images/Rectangle 17323 (2).png",
"isFavourite": false
},
{
"id": "10",
"name": "Ice Cream",
"restaurantName": "Naturals",
"price": "80",
"rating": "5.0",
"totalRatings": "1500",
"image": "assets/images/Rectangle 17324 (2).png",
"isFavourite": false
}
]
};
Map<String, dynamic> get popularDishes {
return {..._popularDishes};
}
Map<String, dynamic> getProductById(String id) { //The filter method that I've tried to write that yields the error
return _popularDishes["data"].where((value) => value.id == id).toList();
}这就是我如何使用提供程序调用getProductById方法。routes['id']基本上是上一页中用作路由参数的id。id来自与此问题相同的JSON数据。
class CartScreenState extends State<CartScreen> {
@override
Widget build(BuildContext context) {
final width = MediaQuery.of(context).size.width;
final height = MediaQuery.of(context).size.height;
final textScale = MediaQuery.of(context).textScaleFactor * 1.2;
final routes =
ModalRoute.of(context)!.settings.arguments as Map<String, dynamic>;
routes['id']
final id = routes['id'];
final provider =
Provider.of<PopularDishesProvider>(context).getProductById(id);https://stackoverflow.com/questions/71275958
复制相似问题