【问题标题】:Dart: function's parameter notationDart:函数的参数表示法
【发布时间】:2018-08-22 11:50:38
【问题描述】:

我有时会发现这样的东西:

Future<List<Photo>> fetchPhotos(http.Client client) async {
final response = await client.get('https://jsonplaceholder.typicode.com/photos');
return compute(parsePhotos, response.body);
}

parsePhotos 函数在哪里:

List<Photo> parsePhotos(String responseBody) {
final parsed = json.decode(responseBody).cast<Map<String, dynamic>>();
return parsed.map<Photo>((json) => Photo.fromJson(json)).toList();
}

我无法理解compute(parsePhotos, response.body)parePhotos 函数接受了 responseBody 参数,但是,正如 compute 所写,似乎没有收到它。那么,有人可以解释一下这个符号吗? 附:希望它足够清楚。

【问题讨论】:

    标签: function parameters dart notation


    【解决方案1】:

    return compute(parsePhotos, response.body);
    

    parsePhotosresponse.body 只是两个独立的参数。 第一个是传递给computes callback参数的parsePhotos函数的引用,第二个是传递给compute函数的message参数的来自client.get(...)的响应数据.

    compute 所做的是以parsePhotos 作为入口点创建一个新的隔离(就像主隔离的main()),然后将message 作为参数传递给它。

    所以 不是这条线 return compute(parsePhotos, response.body);response.body 传递到 parsePhotos 而是

    final Isolate isolate = await Isolate.spawn(
        _spawn,
        new _IsolateConfiguration<Q, R>(
          callback,
          message,
    

    来自compute 实现https://docs.flutter.io/flutter/foundation/compute.html

    【讨论】:

    • 感谢您的解释!
    猜你喜欢
    • 1970-01-01
    • 2020-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多