首页 > 解决方案 > 无法将参数类型“”分配给参数类型 List <>

问题描述

我正在尝试从函数中获取学生列表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();
  }
}

标签: dart

解决方案


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

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

相反,List如果您只想使用一个列表,TotalStudents请使用以下内容:

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

推荐阅读