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


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.

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

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)
        .then((value) =>
            .map((e) =>
                NewsHistoryModel.fromJson( as Map<String, dynamic>))

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 = as List<NewsHistoryModel>;

How can I assign the firestore object properly to List<NewsHistoryModel>. Any help will be very appreciated.


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.