'Populate data Map before opening screen

I am trying to get data from an API to populate a Map and then display the screen to show the data from the Map

My getter is as follows:

Map data = {};
Future getData(symbol) async {
  final response = await http.get(Uri.parse(
      "https://financialmodelingprep.com/api/v3/quote/" +
          symbol +
          "?apikey=${api_key}"));
  if (response.statusCode == 200) {
    final jsonResponse = json.decode(response.body);
    // print(jsonResponse);
    data = jsonResponse[0];
    print(data);
  }
}

For starters, I am trying to display the symbol and name from the data in the API:

void initState() {
    getData(data['symbol']);
    super.initState();
  }

Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        toolbarHeight: 80,
        centerTitle: true,
        backgroundColor: Colors.white,
        automaticallyImplyLeading: true,
        title: Column(
          children: [
            Text("Symbol", style: TextStyle(color: Colors.black)),
            Text("Name", style: TextStyle(color: Colors.black))
          ],
        ),
      ),
    );
  }

After opening one screen successfully, on opening the next screen, I get the error:

type 'Null' is not a subtype of type 'String'

Can anyone suggest steps to deal with the issue? Any help would be appreciated.

Thanks



Sources

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

Source: Stack Overflow

Solution Source