【问题标题】:The operator '[]' isn't defined for the type 'Iterable<Meal>'. Try defining the operator '[]'.dartundefined_operator没有为类型“Iterable<Meal>”定义运算符“[]”。尝试定义运算符 '[]'.dartundefined_operator
【发布时间】:2020-07-23 21:21:54
【问题描述】:

我尝试在另一个类的另一个小部件中返回 MealItem 类的构造函数,我正确地导入了,我得到了这个

错误没有为“Iterable”类型定义运算符“[]”。尝试定义运算符 '[]'.dartundefined_operator

这里是 MealItem 类

import 'package:flutter/material.dart';
import '../models/meal.dart';

class MealItem extends StatelessWidget {
  final String title;
  final String imageUrl;
  final int duration;
  final Complexity complexity;
  final Affordability affordability;

  MealItem(
      this.title,
      this.imageUrl,
      this.duration,
      this.complexity,
      this.affordability
    );
  }
}

这是 CategoryMealsScreen 类中的错误

import 'package:flutter/material.dart';
import '../widgets/meal_item.dart';
import '../models/dummy_data.dart';

class CategoryMealsScreen extends StatelessWidget {
  static const routeName = '/CategoriesScreen';

  //final String categoryId;
  //final String categoryTitle;

  //CategoryMealsScreen(this.categoryId,this.categoryTitle);

  @override
  Widget build(BuildContext context) {
    final routeArgs =
        ModalRoute.of(context).settings.arguments as Map<String, String>;
    final categoryTitle = routeArgs['title'];
    final categoryId = routeArgs['id'];
    final categoryMeals = DUMMY_MEALS.where((meal) {
      return meal.categories.contains(categoryId);
    });
    return Scaffold(
      appBar: AppBar(title: Text(categoryTitle)),
      body: ListView.builder(
        itemBuilder: (ctx, index) {
          return MealItem(
              title : categoryMeals[index].title,
              imageUrl: categoryMeals[index].imageUrl,
              duration: categoryMeals[index].duration,
              complexity: categoryMeals[index].complexity,
              affordability: categoryMeals[index].affordability
           );
        },
        itemCount: categoryMeals.length,
      ),
    );
  }
}

这是使用 vsCode 的 IDE 错误:

The operator '[]' isn't defined for the type 'Iterable<Meal>'.
Try defining the operator '[]'.

任何帮助将不胜感激

【问题讨论】:

    标签: flutter dart operators


    【解决方案1】:

    您将 命名 参数传递给构造函数,而该构造函数采用 位置 参数。

    变化:

    import 'package:flutter/material.dart';
    import '../models/meal.dart';
    
    class MealItem extends StatelessWidget {
      final String title;
      final String imageUrl;
      final int duration;
      final Complexity complexity;
      final Affordability affordability;
    
      // use parenthesis '{}' to wrap the constructor arguments to make them named arguments
      MealItem({
          this.title,
          this.imageUrl,
          this.duration,
          this.complexity,
          this.affordability
        });
      }
    }
    

    或者在创建 MealItem 时简单地删除名称并传递位置参数

    另外:位置参数默认为required,但命名参数不是。将它们更改为命名参数后,如果需要,不要忘记将它们标记为@required

    这里也使用toList()

    final categoryMeals = DUMMY_MEALS.where((meal) {
      return meal.categories.contains(categoryId);
    }).toList();
    

    【讨论】:

    • 感谢您的帮助,但 CategoryMealsScreen 中的索引仍然有错误
    • 所以你有不止一个错误,剩下的一个不在你共享的代码中
    • @AbdessamadJadid 它可能与 categoryMeals 有关,尝试在您定义它的位置粘贴一些代码
    • 完成,我添加 CategoryMealsItem 类和下面的错误消息
    • @AbdessamadJadid 抱歉耽搁了,我更新了答案以包含解决方案的其余部分
    猜你喜欢
    • 2021-07-30
    • 1970-01-01
    • 1970-01-01
    • 2021-05-11
    • 2021-04-03
    • 2021-06-11
    • 2021-10-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多