我是 flutter 新手,一直在开发一个小型 api,可以从我创建的本地模拟数据库中提取数据。数据库中有一个嵌套的 Json,无论我做什么,我都无法让它输出除“类的实例”之外的任何内容,而不是其中的实际 Json。该代码从 Json 获取数据,该 Json 包含有关健身房锻炼以及完成的组数和次数的模拟数据
这是第一个模型:
import 'package:json_annotation/json_annotation.dart';
import 'package:workout_card_api/src/models/sets.dart';
part 'exercise.g.dart';
@JsonSerializable(explicitToJson: true)
class Exercise {
Exercise({
required this.exerciseId,
required this.exerciseName,
required this.exerciseType,
required this.setList,
});
int exerciseId;
String exerciseName;
String exerciseType;
List<Sets> setList;
factory Exercise.fromJson(Map<String, dynamic> json) =>
_$ExerciseFromJson(json);
Map<String, dynamic> toJson() => _$ExerciseToJson(this);
}
这是第二个模型:
import 'package:json_annotation/json_annotation.dart';
part 'sets.g.dart';
@JsonSerializable()
class Sets {
Sets({required this.setId, required this.weightKg, required this.reps});
int setId;
int weightKg;
int reps;
factory Sets.fromJson(Map<String, dynamic> json) => _$SetsFromJson(json);
Map<String, dynamic> toJson() => _$SetsToJson(this);
}
这是测试代码:
void main() {
group('Sets', () {
late Map<String, dynamic> json;
setUp(() {
json = {'setId': 1, 'weightKg': 30, 'reps': 15};
});
test('set object correct properties', () {
final setData = Sets.fromJson(json);
expect(setData.setId, 1);
expect(setData.weightKg, 30);
expect(setData.reps, 15);
});
});
group('Exercise', () {
late Map<String, dynamic> json;
setUp(() {
json = {
'exerciseId': 2,
'exerciseName': 'Bicep Curl',
'exerciseType': 'Arms',
'setList': [
{'setId': 1, 'weightKg': 30, 'reps': 15},
{'setId': 2, 'weightKg': 30, 'reps': 14},
{'setId': 3, 'weightKg': 30, 'reps': 12}
]
};
});
test('exercise object correct properties', () {
final exerciseData = Exercise.fromJson(json);
expect(exerciseData.exerciseId, 2);
expect(exerciseData.exerciseName, 'Bicep Curl');
expect(exerciseData.exerciseType, 'Arms');
expect(exerciseData.setList, [
{'setId': 1, 'weightKg': 30, 'reps': 15},
{'setId': 2, 'weightKg': 30, 'reps': 14},
{'setId': 3, 'weightKg': 30, 'reps': 12}
]);
});
});
}
第一部分测试工作正常的集合模型。第二部分测试运动模型,该模型始终输出以下内容:
Expected: [
{'setId': 1, 'weightKg': 30, 'reps': 15},
{'setId': 2, 'weightKg': 30, 'reps': 14},
{'setId': 3, 'weightKg': 30, 'reps': 12}
]
Actual: [Instance of 'Sets', Instance of 'Sets', Instance of 'Sets']
Which: at location [0] is <Instance of 'Sets'> which expected a map
我尝试了很多方法,例如将类型从 Map
发生错误是因为expect函数将Exercise的setList与map列表进行比较,但setList实际上是Sets对象的列表。
解决方案如下:
test('exercise object correct properties', () {
final exerciseData = Exercise.fromJson(json);
expect(exerciseData.exerciseId, 2);
expect(exerciseData.exerciseName, 'Bicep Curl');
expect(exerciseData.exerciseType, 'Arms');
expect(exerciseData.setList.length, 3);
// Check the properties of each set
expect(exerciseData.setList[0].setId, 1);
expect(exerciseData.setList[0].weightKg, 30);
expect(exerciseData.setList[0].reps, 15);
expect(exerciseData.setList[1].setId, 2);
expect(exerciseData.setList[1].weightKg, 30);
expect(exerciseData.setList[1].reps, 14);
expect(exerciseData.setList[2].setId, 3);
expect(exerciseData.setList[2].weightKg, 30);
expect(exerciseData.setList[2].reps, 12);
});