Home > Back-end >  type 'List<NewsHistoryModel>' is not a subtype of type 'List<NewsHistoryMode
type 'List<NewsHistoryModel>' is not a subtype of type 'List<NewsHistoryMode

Time:11-05

i am fairly new to flutter world and currently stuck at a type casting. i think i am doing it right but flutter thinks i am not that is why is throwing an error when i compile the code. here is what i am doing.

   FutureBuilder(
        future: newsHistoryService.newsHistoryByCategoryType(
            newsType: dropdownValue),
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            List<NewsHistoryModel> data = snapshot.data as List<NewsHistoryModel>;
            return data.isNotEmpty
                ? ListView.builder(
                  )
                : emptyWidget();
          }
          return snapWidgetHelper(snapshot,
              defaultErrorMessage: errorSomethingWentWrong);
        },
      ).paddingAll(16)

newsHistoryService.newsHistoryByCategoryType function as follow.

  Future<List<NewsHistoryModel>> newsHistoryByCategoryType( {String? newsType}) async {
    return await ref!
        .where(NewsHistoryKeys.userId, isEqualTo: appStore.userId)
        .where(NewsHistoryKeys.newType, isEqualTo: newsType)
        .orderBy(CommonKeys.createdAt, descending: true)
        .get()
        .then((value) => value.docs
            .map((e) =>
                NewsHistoryModel.fromJson(e.data() as Map<String, dynamic>))
            .toList());
  }

flutter throws an error as follow.

Another exception was thrown: type 'List<NewsHistoryModel>' is not a subtype of type 'List<NewsHistoryModel>' in type cast.

the line responsible for this error is

List<NewsHistoryModel> data = snapshot.data as List<NewsHistoryModel>;

how can i assign the firestore object properly to List<NewsHistoryModel>. any help will be very appreciated.

CodePudding user response:

The problem here is Firebase stores lists as Dynamic List. What you can try to do is;

   FutureBuilder(
    future: newsHistoryService.newsHistoryByCategoryType(
        newsType: dropdownValue),
    builder: (context, snapshot) {
      if (snapshot.hasData) {
        List<NewsHistoryModel> data = snapshot.data.cast<List<NewsHistoryModel>>;
        return data.isNotEmpty
            ? ListView.builder(
              )
            : emptyWidget();
      }
      return snapWidgetHelper(snapshot,
          defaultErrorMessage: errorSomethingWentWrong);
    },
  ).paddingAll(16)

Or if this doesn't work you can try to modify the factory method you have for NewHistoryModel and add .cast to necessary fields.

CodePudding user response:

You can try

List<NewsHistoryModel> data =  List<NewsHistoryModel>.from(snapshot.data);

or also change

value.docs
        .map((e) =>
            NewsHistoryModel.fromJson(e.data() as Map<String, dynamic>))
        .toList());

to:

List<NewsHistoryModel>.from(value.docs
        .map((e) =>
            NewsHistoryModel.fromJson(Map<String, dynamic>.from(e.data())))
        .toList()));
  • Related