I am having trouble using compute function. I have one higher order function like below;
Future<DistrictTimeData> getDistrictTimeData(String path) async {
Uri url = Uri.parse(ApiConstants.instance.baseUrl "/$path");
var res = await http.get(url);
if (res.statusCode != 200) throw Exception('Bir hata oluştu.');
return DistrictTimeData.fromJson(jsonDecode(res.body));
}
I am able to work this function in compute like this;
@override
void init() async{
changeLoading();
districtTimeData=await compute(NetworkManager.instance!.getDistrictTimeData,path);
changeLoading();
}
The problem starts with the function below which is not a higher order;
Future<List> getTimeZones() async {
Uri url = Uri.parse(ApiConstants.instance.baseUrl);
var res = await http.get(url);
if (res.statusCode != 200) throw Exception('Bir hata oluştu.');
return jsonDecode(res.body);
}
When I try to compute this function it's excepting 1 more argument which is suppose to be the parameter for the fuction that I'm trying to compute. There is no such an argument for that function since it's not an higher order function. My question is what is the way to compute the function that is not a higher order ? Thanks in advance.
CodePudding user response:
It's best not to use a closure such as (_) => getTimeZones
for this, as is explained in the compute
documentation:
Using closures must be done with care. Due to dart-lang/sdk#36983 a closure may captures objects that, while not directly used in the closure itself, may prevent it from being sent to an isolate.
Instead, a simple wrapper function can be made:
Future<List> computeTimeZones(void _) => getTimeZones();