【发布时间】:2020-08-01 19:18:19
【问题描述】:
所以我在这里有一个未来函数,获取一个 json 文件,对其进行解析并将其附加到一个列表中。我想将数据返回给我拥有的另一个类,这样当我在另一个类上调用函数时,我会从函数中获取返回值。我该怎么做?
这是我的函数 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;
}
这是我在另一个类 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]),
);
});
}
【问题讨论】: