无法在dart中访问List中对象的成员变量

时间:2014-10-22 16:25:26

标签: dart angular-dart

我的组件中有一个团队列表。这些团队列在组件的html模板中。我已经创建了添加和更新功能,但是如果我尝试访问团队的成员变量,我会得到错误:NoSuchMethodError:undefined不是函数:

void delete(int id, String name, int onlineId){
Team tempTeam;
  html.window.console.info("In delete()");
  for(int i = 0; i < teamList.length; i++){
    if(teamList.elementAt(i).id == selectedTeam){ // <-- this fails
      tempTeam = teamList.elementAt(i);
    }
  }
  ...
  html.window.console.info("Deleting team: " + name + " succeded!");
}

以下是组件:

@Component(
    selector: 'admin-teams-view',
    templateUrl: 'packages/fremad/components/admin/teams_view.html',
    cssUrl: 'packages/fremad/components/admin/teams_view.css',
    useShadowDom: false
)
class ShowAdminTeamsComponent {
  List<Team> teamList;
  int selectedTeam = -1;
  bool isEditing = false;
  ...
}

以下是感兴趣的HTML

<li ng-repeat="team in teamList">
  <div class="item-name" ng-click="selectTeam(team.id)">
  {{team.name}}
  </div>
  <div class="item-edit" ng-hide="isActive(team.id)">
    <form>
      Name:
      <input type="text" size="35" ng-model="team.name">
      Online ID:
      <input type="number" size="15" ng-model="team.onlineId">
      <input type="button" class="adminButton redButton" value="delete" ng-click="delete(team.id, team.name, team.onlineId)">
    </form>
  </div>
</li>

我会感谢任何帮助!

执行打印后的控制台(teamList [i])和print(teamList [i] .name):

{id: 1, name: Fremad Famagusta Menn Senior A, onlineId: 30296} js_primitives.dart:25
NoSuchMethodError: undefined is not a function

STACKTRACE:
TypeError: undefined is not a function
    at dart.J.get$name$x (http://localhost:8080/fremad/fremad_main.dart.js:40910:39)
    at ShowAdminTeamsComponent.delete$3 (http://localhost:8080/fremad/fremad_main.dart.js:34640:22)
    at eval (eval at <anonymous> (http://localhost:8080/fremad/fremad_main.dart.js:2922:12), <anonymous>:2:42)
    at dart.Primitives_applyFunction (http://localhost:8080/fremad/fremad_main.dart.js:2609:23)
    at StaticClosureMap_lookupFunction_closure.call$3 (http://localhost:8080/fremad/fremad_main.dart.js:11389:18)
    at ClosureMapLocalsAware_lookupFunction_closure.call$3 (http://localhost:8080/fremad/fremad_main.dart.js:9546:79)
    at CallScope.methodClosure$3 (http://localhost:8080/fremad/fremad_main.dart.js:9851:33)
    at CallScope.eval$2 (http://localhost:8080/fremad/fremad_main.dart.js:9868:19)
    at _UnwrapExceptionDecorator.eval$2 (http://localhost:8080/fremad/fremad_main.dart.js:9359:31)
    at _UnwrapExceptionDecorator.eval$2 [as eval$1] (http://localhost:8080/fremad/fremad_main.dart.js:9372:19) 

编辑: 这就是我的TeamList的加载方式:

void loadData() {
  tableLoaded = false;
  _http.get('rest/team/getTeams.json')
    .then((HttpResponse response) {
      teamListObject = new TeamList.fromJson(response.data);
      teamList = teamListObject.teamList;
      tableLoaded = true;
      html.window.console.info("Success on loading table");
    })
    .catchError((e) {
      print(e);
      tableLoaded = false;
    });
} 

这是班级:

class TeamList {
  bool empty;
  List<Team> teamList;

  TeamList(this.empty, this.teamList);

  Map<String, dynamic> toJson() => <String, dynamic>{
    "emtpy" : empty,
    "listObject": teamList
  };

  TeamList.fromJson(Map<String, dynamic> json) : this( 
      json['empty'], json['listObject']);
}

3 个答案:

答案 0 :(得分:3)

您需要更改TeamList类中的fromJson:

TeamList.fromJson(Map<String, dynamic> json) : this( 
    json['empty'], new List<Team>.from(json['listObject'].map((x) => new Team.fromJson(x))));

Dart不会以这种方式理解嵌套类。您已尝试将地图listObject作为地图访问,而不是List。

答案 1 :(得分:0)

您收到了什么确切的错误消息? 您应该尝试在for循环中转储列表和索引 - 使用print()。

答案 2 :(得分:-1)