Home > Enterprise >  How to fix type 'Future<RssFeed>' is not a subtype of type 'RssFeed?' erro
How to fix type 'Future<RssFeed>' is not a subtype of type 'RssFeed?' erro

Time:01-05

I am creating a podcast streaming app and am getting the error "type 'Future' is not a subtype of type 'RssFeed?' " as I navigate to my podcast data screen which loads but with no data present.

The error occurs as I try to assign the returned RssFeed from my initData() function (using the webfeed package) to a variable (showData) of type "RssFeed?" in initState.

The code is below that keeps throwing the error:

@override
  void initState() {
    try {
      showData = initData() as RssFeed;
    } catch (e) {
      print('initPodScreenError: $e');
    }
    super.initState();
  }

  RssFeed? showData;

  Future<RssFeed> initData() async {
    try {
      RssFeed data = await PodcastData().getdata(widget.showURL!);
      return data;
    } catch (e) {
      print('podcastScreenDataError: $e');
      throw Exception('podcastScreenDataError: $e');
    }
  }

I've tried multiple ways of changing up the variable type definitions and no matter what I keep getting this error. The error just changes to include whatever I change anything to whether it be the return type of initData() function or the showData variable

Error:

enter image description here

enter image description here

CodePudding user response:

Try this,

  @override
  void initState() {
    initData();
    super.initState();
  }

  RssFeed? showData;

  Future<void> initData() async {
    try {
      RssFeed data = await PodcastData().getdata(widget.showURL!);
      setState((){
       showData = data;
      });
    } catch (e) {
      print('podcastScreenDataError: $e');
    }
  }

CodePudding user response:

You can make initState async. It would be better if you use FutureBuilder.

Also can be done.

@override
  void initState() {
   super.initState();
    try {
      initData().then((value){
       showData= value;
       setState((){}) ;
       });
    } catch (e) {
      print('initPodScreenError: $e');
    }
  }
  • Related