gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 01:44:29 30 4
gpt4 key购买 nike

我的组件中有一个团队列表。这些团队在组件的 html 模板中列出。我已经创建了添加和更新函数,它们正在运行,但是如果我尝试访问团队的成员变量,我会收到错误消息:NoSuchMethodError: undefined is not a function:

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>

如有任何帮助,我将不胜感激!

在执行 print(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']);
}

最佳答案

您需要更改 TeamList 类中的 fromJson:

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

Dart 不以这种方式理解嵌套类。您已尝试将 map listObject 作为 map 而不是列表访问。

关于dart - 无法访问dart中List中对象的成员变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26512332/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com