hi I am still learning flutter and I am struggling to understand and fix this problem.
I am using dio to retrieve data from api to pass it to repository then to the cubit and then the ui.
when ever I press button that takes me to screen that suppose to show data that come api.
gives me this error.
debug counsel
flutter: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'FutureOr<List<dynamic>>'
Json respons
{
"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"
}
}
]}
my 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;
}
}
my dio
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 [];
}
}
}
my repository
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();
}
}
my cubit
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;
}
}
CodePudding user response:
In your ApinWebServices, you are using
Dio.get('chapters');
which is not a property of Dio
, as your data is json use
return response.data['chapters'];