gpt4 book ai didi

dart - 未捕获的TypeError:无法在dart中读取nullError的属性 'getInitialConfig'

转载 作者:行者123 更新时间:2023-12-03 04:52:50 25 4
gpt4 key购买 nike

**我是新手,我在这里使用适配器模式,我不知道何时我调用getInitialConfig方法调用主方法时该方法返回null请给我一个帮助**

class below


import 'dart:convert';

// team attribute
class TeamAttributes {
String teamName;
int numOfPlayers;
int wkPlayer;
int captainPlayer;
int currentIndex;
List<String> teamPlayerList = new List<String>();

TeamAttributes({
this.teamName,
this.numOfPlayers,
this.wkPlayer,
this.captainPlayer,
this.currentIndex,
this.teamPlayerList,
});



factory TeamAttributes.fromJson(Map<String, dynamic> json) {
return TeamAttributes(
teamName: json['teamName'],
numOfPlayers: json['numOfPlayers'],
wkPlayer: json['wkPlayer'],
captainPlayer: json['captainPlayer'],
currentIndex: json['currentIndex'],
teamPlayerList: parseList(json['teamPlayerList']), // json['places']);
);
}



Map<String, dynamic> toJson() {
return {
'teamName': teamName,
"numOfPlayers": numOfPlayers,
"wkPlayer": wkPlayer,
"captainPlayer": captainPlayer,
"currentIndex": currentIndex,
"teamPlayerList": teamPlayerList,
};
}

static List<String> parseList(teamPlayerJson) {
List<String> teamPlayerList = new List<String>.from(teamPlayerJson);
return teamPlayerList;
}
}

///setting
class QuickStartSettings {
String matchName;
final BattingRestrictions battingRestrictions;
final BowlingRestrictions bowlingRestrictions;
final Overs overs;
final Balls balls;
final Wides wides;
final NoBalls noBalls;
final Wickets wickets;

QuickStartSettings({
this.matchName,
this.battingRestrictions,
this.bowlingRestrictions,
this.overs,
this.balls,
this.wides,
this.noBalls,
this.wickets,
});

factory QuickStartSettings.fromJson(Map<String, dynamic> json) {
return QuickStartSettings(
matchName: json['matchName'],
battingRestrictions:
BattingRestrictions.fromJson(json['battingRestrictions']),
bowlingRestrictions:
BowlingRestrictions.fromJson(json['bowlingRestrictions']),
overs: Overs.fromJson(json['overs']),
balls: Balls.fromJson(json['balls']),
wides: Wides.fromJson(json['wides']),
noBalls: NoBalls.fromJson(json['noBalls']),
wickets: Wickets.fromJson(json['wickets']),
);
}
Map<String, dynamic> toJson() {
return {
'matchName': matchName,
"battingRestrictions": battingRestrictions,
'bowlingRestrictions': bowlingRestrictions,
"overs": overs,
"balls": balls,
"wides": wides,
"noBalls": noBalls,
"wickets": wickets,
};
}
}

class Wickets {
bool isWicketDismissal;
bool allowBatAgain;
bool lastManStand;
int battingTeamWicketPenalty;
int bowlingTeamWicketBonus;

Wickets({
this.isWicketDismissal,
this.allowBatAgain,
this.lastManStand,
this.battingTeamWicketPenalty,
this.bowlingTeamWicketBonus,
});
factory Wickets.fromJson(Map<String, dynamic> json) {
return Wickets(
isWicketDismissal: json['isWicketDismissal'],
allowBatAgain: json['allowBatAgain'],
lastManStand: json['lastManStand'],
battingTeamWicketPenalty: json['battingTeamWicketPenalty'],
bowlingTeamWicketBonus: json['bowlingTeamWicketBonus'],
);
}

Map<String, dynamic> toJson() {
return {
'isWicketDismissal': isWicketDismissal,
"allowBatAgain": allowBatAgain,
'lastManStand': lastManStand,
"battingTeamWicketPenalty": battingTeamWicketPenalty,
"bowlingTeamWicketBonus": bowlingTeamWicketBonus,
};
}
}

class NoBalls {
bool alwaysRebowl;
bool rebowlLastBall;
bool rebowlForLastOver;
int noBallValue;
bool isBallFacedCountedForBatsman;

NoBalls({
this.alwaysRebowl,
this.rebowlLastBall,
this.rebowlForLastOver,
this.noBallValue,
this.isBallFacedCountedForBatsman,
});
factory NoBalls.fromJson(Map<String, dynamic> json) {
return NoBalls(
alwaysRebowl: json['alwaysRebowl'],
rebowlLastBall: json['rebowlLastBall'],
rebowlForLastOver: json['rebowlForLastOver'],
noBallValue: json['noBallValue'],
isBallFacedCountedForBatsman: json['isBallFacedCountedForBatsman'],
);
}
Map<String, dynamic> toJson() {
return {
'alwaysRebowl': alwaysRebowl,
"rebowlLastBall": rebowlLastBall,
'rebowlForLastOver': rebowlForLastOver,
"noBallValue": noBallValue,
"isBallFacedCountedForBatsman": isBallFacedCountedForBatsman,
};
}
}

class Wides {
bool alwaysRebowl;
bool rebowlLastBall;
bool rebowlForLastOver;
int wideValue;
bool isBallFacedCountedForBatsman;

Wides({
this.alwaysRebowl,
this.rebowlLastBall,
this.rebowlForLastOver,
this.wideValue,
this.isBallFacedCountedForBatsman,
});
factory Wides.fromJson(Map<String, dynamic> json) {
return Wides(
alwaysRebowl: json['alwaysRebowl'],
rebowlLastBall: json['rebowlLastBall'],
rebowlForLastOver: json['rebowlForLastOver'],
wideValue: json['wideValue'],
isBallFacedCountedForBatsman: json['isBallFacedCountedForBatsman'],
);
}

Map<String, dynamic> toJson() {
return {
'alwaysRebowl': alwaysRebowl,
"rebowlLastBall": rebowlLastBall,
'rebowlForLastOver': rebowlForLastOver,
"wideValue": wideValue,
"isBallFacedCountedForBatsman": isBallFacedCountedForBatsman,
};
}
}

class Balls {
int ballsPerOver;
int ballsLastOver;

Balls({
this.ballsPerOver,
this.ballsLastOver,
});
factory Balls.fromJson(Map<String, dynamic> json) {
return Balls(
ballsPerOver: json['ballsPerOver'],
ballsLastOver: json['ballsLastOver'],
);
}

Map<String, dynamic> toJson() {
return {
'ballsPerOver': ballsPerOver,
"ballsLastOver": ballsLastOver,
};
}
}

class Overs {
int numOfOvers;
bool isUnlimitedOvers;

Overs({
this.numOfOvers,
this.isUnlimitedOvers,
});
factory Overs.fromJson(Map<String, dynamic> json) {
return Overs(
numOfOvers: json['numOfOvers'],
isUnlimitedOvers: json['isUnlimitedOvers'],
);
}

Map<String, dynamic> toJson() {
return {
'numOfOvers': numOfOvers,
"isUnlimitedOvers": isUnlimitedOvers,
};
}
}

class BowlingRestrictions {
bool maxOverWarning;
int maxOverWarningCount;

BowlingRestrictions({
this.maxOverWarning,
this.maxOverWarningCount,
});
factory BowlingRestrictions.fromJson(Map<String, dynamic> json) {
return BowlingRestrictions(
maxOverWarning: json['maxOverWarning'],
maxOverWarningCount: json['maxOverWarningCount'],
);
}

Map<String, dynamic> toJson() {
return {
'maxOverWarning': maxOverWarning,
"maxOverWarningCount": maxOverWarningCount,
};
}
}

class BattingRestrictions {
bool maxBallWarning;
int maxBallWarningCount;

bool maxOverWarning;
int maxOverWarningCount;

bool maxRunsWarning;
int maxRunsWarningCount;

bool forceRetAfterWarning;

BattingRestrictions({
this.maxBallWarning,
this.maxBallWarningCount,
this.maxOverWarning,
this.maxOverWarningCount,
this.maxRunsWarning,
this.maxRunsWarningCount,
this.forceRetAfterWarning,
});
factory BattingRestrictions.fromJson(Map<String, dynamic> json) {
return BattingRestrictions(
maxBallWarning: json['maxBallWarning'],
maxBallWarningCount: json['maxBallWarningCount'],
maxOverWarning: json['maxOverWarning'],
maxOverWarningCount: json['maxOverWarningCount'],
maxRunsWarning: json['maxRunsWarning'],
maxRunsWarningCount: json['maxRunsWarningCount'],
forceRetAfterWarning: json['forceRetAfterWarning'],
);
}
Map<String, dynamic> toJson() {
return {
'maxBallWarnings': maxBallWarning,
"maxBallWarningCount": maxBallWarningCount,
"maxOverWarning": maxOverWarning,
"maxOverWarningCount": maxOverWarningCount,
"maxRunsWarnings": maxRunsWarning,
"maxRunsWarningCount": maxRunsWarningCount,
"forceRetAfterWarning": forceRetAfterWarning,
};
}
}

// QuickStartModel

class QuickStartModel {
final TeamAttributes leftTeam;
final TeamAttributes rightTeam;
final QuickStartSettings settings;
String tossWonBy;
String tossDecision;

QuickStartModel({
this.tossWonBy,
this.tossDecision,
this.leftTeam,
this.rightTeam,
this.settings,
});
factory QuickStartModel.fromJson(Map<String, dynamic> json) {
return QuickStartModel(
leftTeam: TeamAttributes.fromJson(json['leftTeam']),
rightTeam: TeamAttributes.fromJson(json['rightTeam']),
settings: QuickStartSettings.fromJson(json['settings']),
tossWonBy: json['tossWonBy'],
tossDecision: json['tossDecision'],
);
}
Map<String, dynamic> toJson() {
return {
'leftTeam': leftTeam,
"rightTeam": rightTeam,
"settings": settings,
"tossWonBy": tossWonBy,
"tossDecision": tossDecision,
};
}
}

moke json class below


//moke Api
class QuickStartApi {
final String _quickStartJson = '''
{
"tossDecision": "xs",
"tossWonBy": "as",
"leftTeam": {
"teamName": "Team A",
"numOfPlayers": 11,
"wkPlayer": 1,
"captainPlayer": 1,
"currentIndex": 1,
"teamPlayerList": [
"Player 1",
"Player 2",
"Player 3",
"Player 4",
"Player 5",
"Player 6",
"Player 7",
"Player 8",
"Player 9",
"Player 10",
"Player 11"
]
},
"rightTeam": {
"teamName": "Team B",
"numOfPlayers": 11,
"currentIndex": 1,
"captainPlayer": 1,
"wkPlayer": 1,
"teamPlayerList": [
"Player 1",
"Player 2",
"Player 3",
"Player 4",
"Player 5",
"Player 6",
"Player 7",
"Player 8",
"Player 9",
"Player 10",
"Player 11"
]
},
"settings": {
"matchName": 'FootBall',
"battingRestrictions": {
"maxBallWarning": true,
"maxBallWarningCount": 5,
"maxOverWarning": true,
"maxOverWarningCount": 7,
"maxRunsWarning": true,
"maxRunsWarningCount": 9,
"forceRetAfterWarning": true
},
"bowlingRestrictions": { "maxOverWarning": true, "maxOverWarningCount": 5 },
"overs": { "isUnlimitedOvers": true, "numOfOvers": 10 },
"balls": { "ballsLastOver": 1, "ballsPerOver": 1 },
"wides": {
"alwaysRebowl": true,
"rebowlLastBall": false,
"rebowlForLastOver": false,
"wideValue": 10,
"isBallFacedCountedForBatsman": false
},
"noBalls": {
"alwaysRebowl": true,
"rebowlLastBall": false,
"rebowlForLastOver": false,
"noBallValue": 10,
"isBallFacedCountedForBatsman": false
},
"wickets": {
"isWicketDismissal": true,
"allowBatAgain": true,
"lastManStand": false,
"battingTeamWicketPenalty": 5,
"bowlingTeamWicketBonus": 5
}
}
}
''';

String getQuickStartJson() {
return _quickStartJson;
}
}

Adapater pattern abstract class below


//Adapater

abstract class QuickStartAdapter {
QuickStartModel getInitialConfig();
}

abstract class implement below


//adapter impl

class QuickStartRemote extends QuickStartAdapter {
final QuickStartApi _api = QuickStartApi();

@override
QuickStartModel getInitialConfig() {
var quickStartJSON = _api.getQuickStartJson();
var quickStartList = _parseQuickStartJson(quickStartJSON);
print(quickStartList);
return quickStartList;
}

QuickStartModel _parseQuickStartJson(String quickStartJSON) {
// final Map<String, dynamic> quickStartDecodedJson =
// json.decode(quickStartJSON);
var quickStartDecodedJson =
json.decode(quickStartJSON) as Map<String, dynamic>;
print(quickStartDecodedJson);
var quickStartModel = QuickStartModel.fromJson(quickStartDecodedJson);
print(quickStartModel.settings.matchName);
return quickStartModel;
}
}

main method call getInitialConfig method below


void main() {
QuickStartAdapter adapter;

adapter.getInitialConfig();
QuickStartModel abc = adapter.getInitialConfig();
print(abc);


}

最佳答案

因为您从未将adapter变量分配给任何值,所以您可以在这里找到问题。

void main() {
QuickStartAdapter adapter; // <- your problem

adapter.getInitialConfig();
QuickStartModel abc = adapter.getInitialConfig();
}

您需要创建一个从 QuickStartAdapter继承的类的实例(因为该类本身是 abstract并将其分配给 adapter变量。

这是解决方案的示例,其中您创建 QuickStartRemote实例(从QuickStartAdapter扩展而来)并将其分配给变量:
void main() {
QuickStartAdapter adapter = QuickStartRemote();

adapter.getInitialConfig();
QuickStartModel abc = adapter.getInitialConfig();
print(abc);
}

更改之后,我还在JSON模拟数据中发现了一个错误,其中:
"matchName": 'FootBall', 

本来应该:
"matchName": "FootBall",

经过这两个修复,它可以在我的机器上运行。

关于dart - 未捕获的TypeError:无法在dart中读取nullError的属性 'getInitialConfig',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61094314/

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