![](/img/trans.png)
[英]type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'FutureOr<List<dynamic>>'
[英]_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'FutureOr<List<dynamic>>
嗨,我仍在学习 flutter,我正在努力理解和解决这个问题。
我正在使用 dio 从 api 检索数据,将其传递给存储库,然后传递给 cubit,然后传递给 ui。
每当我按下带我到屏幕的按钮时,该屏幕假设显示来自 api 的数据。
给我这个错误。
调试顾问
flutter: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'FutureOr<List<dynamic>>'
Json 回复
{
"chapters": [
{
"id": 114,
"revelation_place": "makkah",
"revelation_order": 21,
"bismillah_pre": true,
"name_simple": "An-Nas",
"name_complex": "An-Nās",
"name_arabic": "الناس",
"verses_count": 6,
"pages": [
604,
604
],
"translated_name": {
"language_name": "english",
"name": "Mankind"
}
}
]}
我的 model
class ChapterModel {
List<Chapters>? chapters;
ChapterModel({this.chapters});
ChapterModel.fromJson(Map<String, dynamic> json) {
if (json['chapters'] != null) {
chapters = <Chapters>[];
json['chapters'].forEach((v) {
chapters!.add(new Chapters.fromJson(v));
});
}
}
}
class Chapters {
int? id;
String? revelationPlace;
int? revelationOrder;
bool? bismillahPre;
String? nameSimple;
String? nameComplex;
String? nameArabic;
int? versesCount;
List<int>? pages;
TranslatedName? translatedName;
Chapters(
{this.id,
this.revelationPlace,
this.revelationOrder,
this.bismillahPre,
this.nameSimple,
this.nameComplex,
this.nameArabic,
this.versesCount,
this.pages,
this.translatedName});
Chapters.fromJson(Map<String, dynamic> json) {
id = json['id'];
revelationPlace = json['revelation_place'];
revelationOrder = json['revelation_order'];
bismillahPre = json['bismillah_pre'];
nameSimple = json['name_simple'];
nameComplex = json['name_complex'];
nameArabic = json['name_arabic'];
versesCount = json['verses_count'];
pages = json['pages'].cast<int>();
translatedName = json['translated_name'] != null
? new TranslatedName.fromJson(json['translated_name'])
: null;
}
}
我的迪奥
class ApinWebServices {
static late Dio dio;
static init() {
BaseOptions options = BaseOptions(
baseUrl: baseUrl,
queryParameters: {'language': 'ar'},
receiveDataWhenStatusError: true,
);
dio = Dio(options);
}
Future<List<dynamic>> getALLJuzQuran() async {
try {
Response response = await dio.get('chapters');
return response.data;
} catch (e) {
print(e.toString());
return [];
}
}
}
我的仓库
class ChapterRepository {
final ApinWebServices apinWebServices;
ChapterRepository(this.apinWebServices);
Future<List<ChapterModel>> getALLJuzQuran() async {
final juzQuran = await apinWebServices.getALLJuzQuran();
return juzQuran.map((e) => ChapterModel.fromJson(e)).toList();
}
}
我的肘
class JuzquranCubit extends Cubit<HomeState> {
final ChapterRepository chapterRepository;
late List<ChapterModel> juzQuran = [];
JuzquranCubit(this.chapterRepository) : super(HomeInitial());
List<ChapterModel> getALLJuzQuran() {
chapterRepository.getALLJuzQuran().then((chapters) {
emit(JuzQuranLoaded(chapters: chapters));
this.juzQuran = juzQuran;
});
return juzQuran;
}
}
在您的 ApinWebServices 中,您正在使用
Dio.get('chapters');
这不是Dio
的属性,因为您的数据是 json 使用
return response.data['chapters'];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.