Dart/Flutter 新手。
尝试使用改造飞镖包,但我的测试代码以异常结束:
DioError [DioErrorType.DEFAULT]: type 'String' is not a subtype of type 'Map<String, dynamic>'
我的 pubspec.yaml(片段):
dependencies:
json_serializable: ^3.2.2
json_annotation: 3.0.0
retrofit: any
logger: 0.7.0 #for logging purpose
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2
dev_dependencies:
retrofit_generator: any
build_runner: any
flutter_test:
sdk: flutter
...
我的 API 和类:
import 'package:json_annotation/json_annotation.dart';
import 'package:retrofit/retrofit.dart';
import 'package:dio/dio.dart';
part 'regions.g.dart';
@RestApi(baseUrl: "https://soargbsc.com/rasp/")
abstract class RaspClient {
factory RaspClient(Dio dio) = _RaspClient;
@GET("current.json")
Future<Regions> getRegions();
}
List<Regions> to List<Region>, etc
@JsonSerializable()
class Regions {
String initialRegion;
List<Region> regions;
Airspace airspace;
Regions({this.initialRegion, this.regions, this.airspace});
factory Regions.fromJson(Map<String, dynamic> json) => _$RegionsFromJson(json);
Map<String, dynamic> toJson() => _$RegionsToJson(this);
}
@JsonSerializable()
class Region {
List<String> dates;
String name;
List<String> printDates;
List<Soundings> soundings;
Region({this.dates, this.name , this.printDates, this.soundings
});
factory Region.fromJson(Map<String, dynamic> json) => _$RegionFromJson(json);
Map<String, dynamic> toJson() => _$RegionToJson(this);
}
@JsonSerializable()
class Soundings {
String location;
String longitude;
String latitude;
Soundings({this.location, this.longitude, this.latitude});
factory Soundings.fromJson(Map<String, dynamic> json) => _$SoundingsFromJson(json);
Map<String, dynamic> toJson() => _$SoundingsToJson(this);
}
@JsonSerializable()
class Airspace {
String baseUrl;
List<String> files;
Airspace({this.baseUrl, this.files});
factory Airspace.fromJson(Map<String, dynamic> json) => _$AirspaceFromJson(json);
Map<String, dynamic> toJson() => _$AirspaceToJson(this);
}
我运行了颤振包 pub run build_runner watch 命令,并确认将适当的代码生成到 region.g.dart 中。
当我在下面运行我的测试程序时,我确实看到了通过 LogInterceptor 显示的 Json,但随后得到了异常。
void main() {
test("Get current.json", () async {
final dio = Dio();
dio.interceptors.add(LogInterceptor(responseBody: true));
final client = RaspClient(dio);
client.getRegions().then(expectAsync1(
(regions) => print("Regions: ${regions.regions.length}")
))
;
});
}
异常在 dio.dart 中被抛出
Response<T> assureResponse<T>(response, [RequestOptions requestOptions]) {
if (response is Response<T>) {
response.request = response.request ?? requestOptions;
} else if (response is! Response) {
response = Response<T>(data: response, request: requestOptions);
} else {
T data = response.data; <<<< Exception happens here <<<<<<<
response = Response<T>(
data: data,
headers: response.headers,
request: response.request,
statusCode: response.statusCode,
isRedirect: response.isRedirect,
redirects: response.redirects,
statusMessage: response.statusMessage,
);
}
return response;
}
任何想法这里出了什么问题?我还需要在某处定义转换器吗?