当我尝试从服务器解析json对象时出现此错误。 我试图找到解决方案很多次,但是我什么也找不到。 IDE说此代码中发生此错误,但未告知特定部分。 下面的代码是我模型的代码,该代码请求api对服务模块的响应。
Future getLolBySummonerName(Map<String, String> queryBody) async {
try {
_lolResponse = await _lolService.getLolBySummonerName(queryBody);
notifyListeners();
} on UnauthorizedException {
return Future.error(Navigate('/loginMethod'));
} on NotFoundException {
return Future.error(Message('없는 소환사명입니다.'));
}
}
这是服务模块的一部分。
Future<LolResponse> getLolBySummonerName(Map<String, String> queryBody) async {
var uri = Uri.https('servier-url', '/endpoint', queryBody);
final response = await http.get(uri);
if (response.statusCode == 200) {
return LolResponse.fromJson(jsonDecode(response.body));
}
NetworkErrorHandler.throwProperException(response);
return null;
}
最后,这些类是服务器的响应。
class LolResponse {
final String evaluation;
final FlexRank flexRank;
final String icon;
final int level;
final List<Most> mosts;
final SoloRank soloRank;
final String summonerName;
LolResponse(
{this.evaluation,
this.flexRank,
this.icon,
this.level,
this.mosts,
this.soloRank,
this.summonerName});
factory LolResponse.fromJson(Map<String, dynamic> json) {
return LolResponse(
evaluation: json['evaluation'],
flexRank: FlexRank.fromJson(json['flexRank']),
icon: json['icon'],
level: json['level'],
mosts: json['mosts'].map((e) => Most.fromJson(e)).toList(),
soloRank: SoloRank.fromJson(json['soloRank']),
summonerName: json['summonerName']);
}
}
class FlexRank {
final String image;
final String name;
FlexRank({this.image, this.name});
factory FlexRank.fromJson(Map<String, dynamic> json) {
if (json == null) return FlexRank();
return FlexRank(image: json['image'], name: json['name']);
}
}
class Most {
final String evaluation;
final int gameCount;
final String image;
final String name;
final double winRate;
Most({this.evaluation, this.gameCount, this.image, this.name, this.winRate});
factory Most.fromJson(Map<String, dynamic> json) {
if (json == null) return Most();
return Most(
evaluation: json['evaluation'],
gameCount: json['gameCount'],
image: json['image'],
name: json['name'],
winRate: json['winRate']);
}
}
class SoloRank {
final String image;
final String name;
SoloRank({this.image, this.name});
factory SoloRank.fromJson(Map<String, dynamic> json) {
if (json == null) return SoloRank();
return SoloRank(image: json['image'], name: json['name']);
}
}
我写了一些扑扑的信息,如果这可以帮助我解决问题。
扑打医生
[√] Flutter (Channel stable, 1.20.1, on Microsoft Windows [Version 10.0.19041.388], locale ko-KR)
[√] Android toolchain - develop for Android devices (Android SDK version 30.0.0)
[√] Android Studio (version 4.0)
[!] IntelliJ IDEA Community Edition (version 2019.2)
X Flutter plugin not installed; this adds Flutter specific functionality.
X Dart plugin not installed; this adds Dart specific functionality.
[√] VS Code (version 1.47.3)
[√] Connected device (1 available)
! Doctor found issues in 1 category.
添加
我把整个错误日志都带到了这里。
E/flutter (22270): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: type '_CompactLinkedHashSet<void>' is not a subtype of type 'FutureOr<Map<dynamic, dynamic>>'
E/flutter (22270): #0 _FutureListener.handleError (dart:async/future_impl.dart:160:20)
E/flutter (22270): #1 Future._propagateToListeners.handleError (dart:async/future_impl.dart:708:47)
E/flutter (22270): #2 Future._propagateToListeners (dart:async/future_impl.dart:729:24)
E/flutter (22270): #3 Future._completeError (dart:async/future_impl.dart:537:5)
E/flutter (22270): #4 _AsyncAwaitCompleter.completeError (dart:async-patch/async_patch.dart:47:15)
E/flutter (22270): #5 InputProfileModel.getLolBySummonerName (package:match_making/ui/input/input_profile_model.dart)
E/flutter (22270): <asynchronous suspension>
E/flutter (22270): #6 _InputLolBodyState.build.<anonymous closure> (package:match_making/ui/input/lol/component/input_lol_body.dart:43:30)
E/flutter (22270): <asynchronous suspension>
E/flutter (22270): #7 _InputLolBodyState.build.<anonymous closure> (package:match_making/ui/input/lol/component/input_lol_body.dart)
E/flutter (22270): #8 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:992:19)
E/flutter (22270): #9 _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:1098:38)
E/flutter (22270): #10 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:184:24)
E/flutter (22270): #11 TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:524:11)
E/flutter (22270): #12 BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:284:5)
E/flutter (22270): #13 BaseTapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:256:7)
E/flutter (22270): #14 GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:158:27)
E/flutter (22270): #15 GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:224:20)
E/flutter (22270): #16 GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:200:22)
E/flutter (22270): #17 GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:158:7)
E/flutter (22270): #18 GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:104:7)
E/flutter (22270): #19 GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:88:7)
E/flutter (22270): #20 _rootRunUnary (dart:async/zone.dart:1206:13)
E/flutter (22270): #21 _CustomZone.runUnary (dart:async/zone.dart:1100:19)
E/flutter (22270): #22 _CustomZone.runUnaryGuarded (dart:async/zone.dart:1005:7)
E/flutter (22270): #23 _invoke1 (dart:ui/hooks.dart:267:10)
E/flutter (22270): #24 _dispatchPointerDataPacket (dart:ui/hooks.dart:176:5)
E/flutter (22270):
也是JSON请求格式
{
"evaluation": "string",
"flexRank": {
"image": "string",
"name": "string"
},
"icon": "string",
"level": 0,
"mosts": [
{
"evaluation": "string",
"gameCount": 0,
"image": "string",
"name": "string",
"winRate": 0
}
],
"soloRank": {
"image": "string",
"name": "string"
},
"summonerName": "string"
}
答案 0 :(得分:0)
class LolResponse {
String evaluation;
FlexRank flexRank;
String icon;
dynamic level;
List<Mosts> mosts;
FlexRank soloRank;
String summonerName;
LolResponse(
{this.evaluation,
this.flexRank,
this.icon,
this.level,
this.mosts,
this.soloRank,
this.summonerName});
LolResponse.fromJson(Map<String, dynamic> json) {
evaluation = json['evaluation'];
flexRank = json['flexRank'] != null
? new FlexRank.fromJson(json['flexRank'])
: null;
icon = json['icon'];
level = json['level'];
if (json['mosts'] != null) {
mosts = new List<Mosts>();
json['mosts'].forEach((v) {
mosts.add(new Mosts.fromJson(v));
});
}
soloRank = json['soloRank'] != null
? new FlexRank.fromJson(json['soloRank'])
: null;
summonerName = json['summonerName'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['evaluation'] = this.evaluation;
if (this.flexRank != null) {
data['flexRank'] = this.flexRank.toJson();
}
data['icon'] = this.icon;
data['level'] = this.level;
if (this.mosts != null) {
data['mosts'] = this.mosts.map((v) => v.toJson()).toList();
}
if (this.soloRank != null) {
data['soloRank'] = this.soloRank.toJson();
}
data['summonerName'] = this.summonerName;
return data;
}
}
class FlexRank {
String image;
String name;
FlexRank({this.image, this.name});
FlexRank.fromJson(Map<String, dynamic> json) {
image = json['image'];
name = json['name'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['image'] = this.image;
data['name'] = this.name;
return data;
}
}
class Mosts {
String evaluation;
dynamic gameCount;
String image;
String name;
int winRate;
Mosts({this.evaluation, this.gameCount, this.image, this.name, this.winRate});
Mosts.fromJson(Map<String, dynamic> json) {
evaluation = json['evaluation'];
gameCount = json['gameCount'];
image = json['image'];
name = json['name'];
winRate = json['winRate'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['evaluation'] = this.evaluation;
data['gameCount'] = this.gameCount;
data['image'] = this.image;
data['name'] = this.name;
data['winRate'] = this.winRate;
return data;
}
}
您可以尝试使用此模型并检查您的api是否正常工作。