0
votes

So I have a future function here, to get a json file, parsed it and append it to a list. I want to return the data to another class that i have, so that when i call the function on that other class, i would get the return value from the function. How do i do that?

here is my function class1.dart

Future search(String stock) async {
    print("Starting get request");
    data.clear();
    http.get("https://www.alphavantage.co/query?function=SYMBOL_SEARCH&keywords=" + stock + "&apikey=demo").then((res){
      print("received response.");
      final search = searchFromJson(res.body);
      for(int i = 0; i < search.bestMatches.length; i++)
      {
          data.add(search.bestMatches[i].the1Symbol);
          data.add(search.bestMatches[i].the2Name);
      }
    }).catchError((e) {
      print("Failed to get response. " + e.toString());
    });
    return data;
  }

here is my other function on another class class2.dart

  void  _infoModal(context, val) async{
    List<String> data = await _vantage.search(val);
    showModalBottomSheet(context: context, builder: (BuildContext bc) {
      return Container(
        child: Text(data[0]),
      );
    });
  }
1

1 Answers

0
votes

To call the function/method from other class, you should have a reference of that class object or you create a new object like:

Vantage _vantage = new Vantage();

But the problem with the first option is you can't call if you have use Navigator.pushReplacement, which means your page is not on the widget tree.

Personally i will suggest you to create a Helper Class instead of passing class objects.