type 'List<dynamic>' is not a subtype of type 'List<String>' flutter

Viewed 161

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'],
    );
  }
}
2 Answers

I ran your code with some dummy data, the cast<String> for categories works for me. However, you have not added the cast to skills and otherLanguages. Have you checked the line number of the error? If the problem is definitely with categories, could you please add some sample data to the question.

Try to replace List<String> categories, skills, otherLanguages; to dynamic and remove the casting

List<dynamic> categories, skills, otherLanguages;

Related