Flutter _CastError (Null check operator used on a null value) when referencing API

Viewed 86

I'm using this API and trying to save the API 'title' data as a recipeTitle variable and use it as the AppBar title. For some reason when I try to call the recipeTitle variable in the AppBar (5th line from the bottom) it presents an error _CastError (Null check operator used on a null value). Any help is much appreciated.

Thank you!

API file data:

enter image description here

selectedRecipe.dart file:

class _selectedRecipeState extends State<selectedRecipe> {

String recipeURL = "https://api.spoonacular.com/recipes/716429/information?apiKey=APIKEY";
List? listResponse;
Map? mapResponse;
List? listOfResults;
String? id;
Uri? sourceUrl;
String? recipeTitle;

Future fetchData() async {
  http.Response response;
  response = await http 
  .get(Uri.parse(recipeURL));

  if (response.statusCode == 200) {
    setState(() {
      mapResponse = json.decode(response.body);
      sourceUrl=(listOfResults = mapResponse!['sourceUrl']);
      recipeTitle=(listOfResults = mapResponse!['title']);
    });
  }
}
@override
void initState(){
  fetchData();
  super.initState();
  fetchData();
}

@override
Widget build(BuildContext context) {
  return Scaffold(
               appBar: AppBar(
            title:Text(recipeTitle!,
              style: TextStyle(
              fontFamily: 'Fredoka',
              ),
              ),
0 Answers
Related