无法将参数类型“ ”分配给参数类型列表 <>

时间:2021-07-16 07:16:57

标签: dart

我正在尝试从函数 seasonalStudents 中获取学生列表并将其用于另一个函数 studentListener

我想使用 unHapyyStd 但在这行 unHappyStd = [StaticStudents(std.id, null, TotalStudents(std.id, null))]; 我收到错误 The argument type TotalStudents can't be assigned to the parameter Type List<TotalStudents> ,我该如何解决?

class Toppers {
  String id;
  double passmark;
  double failmark;

  Toppers(this.id, this.passmark, this.failmark);
}

class TotalStudents {
  final String id;
  final Image markerIcon;

  TotalStudents(
    this.id,
    this.markerIcon,
  );
}

class StaticStudents {
  final String id;
  final String call;
  final List<TotalStudents> totalStds;

  StaticStudents(this.id, this.call, this.totalStds);
}

class Students {}

class NearPassedStudents {
  String id;
  double passmark;
  double failmark;

  NearPassedStudents(this.id, this.passmark, this.failmark);
}

class GeoStudents {
  static List<NearPassedStudents> passedStudentsList = [];
}

void seasonalStudents() {
  for (NearPassedStudents std in GeoStudents.passedStudentsList) {
    print("student: ${std.id}");
    print("student: ${std.passmark}");
    print("student: ${std.failmark}");

    unHappyStd = [StaticStudents(std.id, null, TotalStudents(std.id, std.passmark))];
  }

  Future<void> studentListener()async{
    ////some method
    /// use it here
    case studentX:
       seasonalStudents();
  }
}

2 个答案:

答案 0 :(得分:1)

您的问题是 StaticStudents 构造函数中的第三个参数需要 List<TotalStudents> 类型,但您发送的是 TotalStudents 对象作为参数:

StaticStudents(std.id, null, TotalStudents(std.id, null))

相反,如果您只想使用具有单个 List 的列表,请使用以下内容将其设为 TotalStudents

StaticStudents(std.id, null, [TotalStudents(std.id, null)])

答案 1 :(得分:1)

因为你的第三个参数需要列表而不是 obj。在你的循环中,你可以这样做

var list = <TotalStudents>[];
list.add(TotalStudents(std.id, null));

unHappyStd = [StaticStudents(std.id, null, list)];