'Why my class2 extends class1 is not a subtype of type class1 in Flutter

I have a Model class and a GameModel extends Model

I don't understand why the model can't convert automatically in GameModel

class Php :

abstract class Php
{
  final String url;
  final Model model;
  late final String function;

  Php(this.url, this.model);

  ///Function to get the response from the server
  Future<Response> _getResponse()
  {
    String theUrl = "http://10.0.2.2/TeamMateProject/php/scripts/"+url+"?function="+function;
    return get(Uri.parse(theUrl));
  }

  ///Function to return a list of models using GET method
  @protected
  Future<List<Model>> phpMethodGetList() async {
    final response = await _getResponse();
    if(response.statusCode==200)
    {
      print(response.body);
      List models = json.decode(response.body);
      return models.map((e) => model.fromMap(e)).toList();
    }
    else
    {
      throw Exception(response.body);
    }
  }

}

class PhpGame extends Php

class PhpGame extends Php
{
  PhpGame() : super("scriptGame.php", GameModel());

  Future<List<GameModel>> getAllGames() async
  {
    function = "getAllGames";
    return phpMethodGetList() as Future<List<GameModel>>;
  }
}

ERROR :

type 'Future<List<Model>>' is not a subtype of type 'Future<List<GameModel>>' in type cast

Here : return phpMethodGetList() as Future<List<GameModel>>;



Solution 1:[1]

I think you would first have to interpret Model as GameModel before you return it as a Future<List<GameModel>>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 MindStudio