【发布时间】:2021-08-10 23:00:51
【问题描述】:
美好的一天! 请帮我找出这个小测试应用程序中的错误。
模特/人:
class People extends Equatable {
final int charId;
});
@override
List<Object> get props => [
charId,
];
static People fromJson(dynamic json) {
return People(
charId: json['char_id'],
);
}
}
API客户端:
class APIClient {
static const baseUrl = '******';
final http.Client httpClient;
APIClient({
required this.httpClient,
}) : assert(httpClient != null);
Future<People> fetchPeopleAPI(int id) async {
final mainUrl = '***';
final peopleResponse = await this.httpClient.get(Uri.parse(mainUrl));
if (peopleResponse.statusCode != 200) {
throw Exception('error getting people with id');
}
final peopleJson = jsonDecode(peopleResponse.body);
return People.fromJson(peopleJson);
}
}
集团:
class PeopleBloc extends Bloc<PeopleEvent, PeopleState> {
final PeopleRepo peopleRepo;
PeopleBloc({
required this.peopleRepo,
}) : assert(peopleRepo != null),
super(PeopleInitialState());
@override
Stream<PeopleState> mapEventToState(
PeopleEvent event,
) async* {
if (event is PeopleRequestedEvent) {
yield PeopleLoadInProgressState();
try {
final People people = await peopleRepo.getPeople(event.id);
yield PeopleLoadSuccessState(people: people);
} catch (e) {
print("error : $e");
yield PeopleLoadFailureState();
}
}
}
}
正是在 bloc.dart 中,我得到了带有文本“error : type 'String' is not a subtype of type 'int' of 'index'”的异常。请帮忙找出错误。
【问题讨论】:
-
它告诉你错误在哪一行?
-
我在 mapEventToState 中通过 print("error: $e") 收到一条消息
-
如果我使用的不是 char_id,而是字符串名称,评论 char_id 字段我会再次收到一条消息。错误:类型“String”不是“index”类型“int”的子类型
-
可能是api中的原因?因为当我使用另一个公共 API 时没有任何错误