Solved: type 'List<NewsHistoryModel>' is not a subtype of type 'List<NewsHistoryModel>' in type cast

Question

Asked by rnative on November 05, 2021 (source).

I am fairly new to flutter world and currently stuck in a type casting. I think I am doing it right, but flutter thinks I am not, that is why it 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 follows:

  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 follows:

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.

Answer

Question answered by rnative (source).

Sorry it was a stupid mistake. in the newsHistoryService file i imported a wrong model class. thanks for the help everyone.

DART FLUTTER
SHARE: