type 'List<dynamic>' is not a subtype of type 'List<String>' flutter
152
Try to replace List<String> categories, skills, otherLanguages;
to dynamic and remove the casting
List<dynamic> categories, skills, otherLanguages;
Author by
Hammas Ali
Updated on December 29, 2022Comments
-
Hammas Ali over 1 year
I am fetching data from firebase and coverting it with fromJson constructor but it is throwing error. The problem which is causing is the categories List in the model and I have done the casting method and it is still not working. Please help me I am stuck here from 2 days. Any solution is not working for me
This is exception
======== Exception caught by widgets library ======================================================= The following _TypeError was thrown building FutureBuilder<DocumentSnapshot>(dirty, state: _FutureBuilderState<DocumentSnapshot>#6cd83): type 'List<dynamic>' is not a subtype of type 'List<String>' The relevant error-causing widget was: FutureBuilder<DocumentSnapshot> file:///E:/flutterProject/filmmaker/lib/auth_screens/signUp_screens/worker/signUp_screen14.dart:93:30 When the exception was thrown, this was the stack: #0 new UserInfoModel.fromMap (package:filmmaker/resources/models/user_info.dart:88:19) #1 _SignUpScreen14State._nameCountry.<anonymous closure> (file:///E:/flutterProject/filmmaker/lib/auth_screens/signUp_screens/worker/signUp_screen14.dart:102:46) #2 _FutureBuilderState.build (package:flutter/src/widgets/async.dart:773:55) #3 StatefulElement.build (package:flutter/src/widgets/framework.dart:4612:27) #4 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4495:15) ... ====================================================================================================
Here is my future builder widget here i am fetching data from firestore and and sending it to the fromJson constructor
Widget get _nameCountry => FutureBuilder<DocumentSnapshot>( future: FirebaseRepo.instance.fetchWorkerDataFromDb(), builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) { switch (snapshot.connectionState) { case ConnectionState.waiting: return CircleAvatar( radius: 80.0, ); case ConnectionState.done: _userInfoModel = UserInfoModel.fromMap(snapshot?.data?.data()); // Map<String, dynamic> data = snapshot?.data?.data(); return Row( children: <Widget>[ Text('${_userInfoModel.name}, '), Text('${_userInfoModel.country}, '), ], ); default: return Text( snapshot.error, textAlign: TextAlign.center, ); } }, // child: Row( // children: <Widget>[Text('${_userInfoModel.name} ,'), Text('')], // ), );
My model function
class UserInfoModel { String uid, name, email, userName, status, profilePhoto, country, profileState, expert, englishProficiency, title, professionalOverview, phoneNo, hourlyRate,createdAt; bool signUpCheckForEmail; List<String> categories, skills, otherLanguages; List<Map> education, employment; Map companyData,companyContacts; UserInfoModel( {this.uid, this.name, this.email, this.userName, this.status, this.profilePhoto, this.country, this.profileState, this.signUpCheckForEmail, this.categories, this.skills, this.expert, this.education, this.employment, this.englishProficiency, this.otherLanguages, this.title, this.professionalOverview, this.companyData, this.phoneNo, this.hourlyRate, this.companyContacts, this.createdAt}); Map toMap(UserInfoModel user) { var data = Map<String, dynamic>(); data['uid'] = user.uid; data['name'] = user.name; data['email'] = user.email; data['userName'] = user.userName; data['status'] = user.status; data['profilePhoto'] = user.profilePhoto; data['country'] = user.country; data['profileState'] = user.profileState; data['signUpCheckForEmails'] = user.signUpCheckForEmail; data['categories'] = user.categories; data['skills'] = user.skills; data['expert'] = user.expert; data['education'] = user.education; data['employment'] = user.employment; data['english proficiency'] = user.englishProficiency; data['other languages'] = user.otherLanguages; data['professionalOverview'] = user.professionalOverview; data['title'] = user.title; data['companyData'] = user.companyData; data['phoneNo'] = user.phoneNo; data['hourlyRate'] = user.hourlyRate; data['companyContacts'] = user.companyContacts; data['createdAt']= user.createdAt; return data; } factory UserInfoModel.fromMap(Map<String, dynamic> data) { return UserInfoModel( uid: data['uid'], name: data['name'], email: data['email'], userName: data['userName'], status: data['status'], profilePhoto: data['profilePhoto'], country: data['country'], profileState: data['profileState'], signUpCheckForEmail: data['signUpCheckForEmails'], categories: data['categories'].cast<String>(), skills: data['skills'], expert: data['expert'], education: data['education'], employment: data['employment'], englishProficiency: data['english proficiency'], otherLanguages: data['other languages'], professionalOverview: data['professionalOverview'], title: data['title'], companyData: data['companyData'], phoneNo: data['phoneNo'], hourlyRate: data['hourlyRate'], companyContacts: data['companyContacts'], createdAt: data['createdAt'], ); } }
-
Patrick O'Hara about 3 yearsIt is much better to have a strongly typed mode (and fix the mapping code).
-
Hammas Ali about 3 yearsI have put cast to other and now the error is ' type 'List<dynamic>' is not a subtype of type 'List<Map<dynamic, dynamic>>' '
-
Hammas Ali about 3 yearsyes i just tested this and is still not working so now I am doing like this Text('data['name'] '),
-
Hammas Ali about 3 yearsthanks man I just tested again and it work fine. I have merged these both answers and the solution was optimal
-
Patrick O'Hara about 3 yearsI repeat my comment that it is not a good idea to use
dynamic
in a model class, it is better to have strong typing. In my testing the cast worked correctly when applied to all the statements that needed it. -
Patrick O'Hara about 3 yearsThis sounds like a different error, because it says the source is type
List<Map<dynamic,dynamic>>