首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在颤振中使用ID的Fliter JSON地图数据

在颤振中使用ID的Fliter JSON地图数据
EN

Stack Overflow用户
提问于 2022-02-26 10:39:51
回答 2查看 100关注 0票数 0

我很难理解如何使用提供者通过ID过滤数据。我尝试过的方法产生了以下错误:

代码语言:javascript
复制
Class '_InternalLinkedHashMap<String, Object>' has no instance getter 'id'.
Receiver: _LinkedHashMap len:8
Tried calling: id

鉴于我在颤振方面的一点经验,任何帮助或建议都将受到高度赞赏。我所写的代码如下:

代码语言:javascript
复制
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数据。

代码语言:javascript
复制
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);
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71275958

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档