js-interop:如何访问回调函数中的输入数据?

时间:2016-08-12 05:17:41

标签: dart

我已经编写了一个使用javascripts API访问Google表格的包装类(因为Dart版本的Google API无效)。在回调函数中,我可以看到数据存储在Dartium上的调试器中的bodyresults字段中,但无法从Dart访问它们。任何建议将不胜感激。该计划正在使用Dart 1.18。

enter image description here enter image description here

Javascript代码

var googleStorage = new GoogleStorage();
function GoogleStorage() {

    this.CLIENT_ID = 'XXXXXXX';
    this.SCOPES = ["https://www.googleapis.com/auth/spreadsheets.readonly"];
}

GoogleStorage.prototype.authorize = function(handleAuthResult) {
    gapi.auth.authorize( {
        client_id: this.CLIENT_ID, scope: this.SCOPES, immediate: false
    }, handleAuthResult);

    return false;
}

GoogleStorage.prototype.loadTraitUrl = function(handleLoadUrlResult) {
    console.log("loadConnectionUrl");
    var discoveryUrl = 'https://sheets.googleapis.com/$discovery/rest?version=v4';
    gapi.client.load(discoveryUrl).then(handleLoadUrlResult);
}

GoogleStorage.prototype.listTraits = function(successFunc, errorFunc) {

   console.log("listData");

   gapi.client.sheets.spreadsheets.values.get({
        spreadsheetId: 'YYYYY',
        range: 'Taits!B8:AQ',
   }).then(successFunc, errorFunc);

}

Dart Wrapper Class

@JS()
library my_workbench.lib.common.google_storage;

import 'package:js/js.dart';

@JS('googleStorage')
class GoogleStorage {
    external static authorize(Function handler);
    external static traitLoadUrl(Function handler);
    external static listTraits(Function success, Function error);
}

飞镖计划

main() async {
  querySelector("#my_button").onClick.listen(handleEvent);
}

void handleEvent(MouseEvent event) {
  print("Button Clicked");

  GoogleStorage.authorize(
      allowInterop((var authResult) {
          handleAuthResult(authResult);
      })
  );
}

void handleAuthResult(var authResult) {
    print("Successful Authenticated: ${authResult}");

   GoogleStorage.traitLoadUrl(
      allowInterop((_) {
          handleLoadUrlResult();
      })
   );
}

void handleLoadUrlResult() {
    print("Successful loaded URL");
    GoogleStorage.listTraits(
        allowInterop((var response) {
            // var range = response.body;
            print("Success: ${response.body}");
        }), allowInterop((var response) {
            print("Success: ${response}");
    }));
}

1 个答案:

答案 0 :(得分:2)

终于找到了解决方案。问题显然是allowInterop()无法使用包含不受支持的数据类型的js response引起的。通过仅返回数据的数组列表,它完美地工作。 js列表通过allowInterop()正确映射到Dart的列表。

Javascript代码

GoogleStorage.prototype.listTraits = function(successFunc, errorFunc) {

   console.log("listData");

   gapi.client.sheets.spreadsheets.values.get({
       spreadsheetId: 'YYYYY',
       range: 'Taits!B8:AQ',
   }).then(function(response) {
       successFunc(response.result.values);
   }, function(response) {
       errorFunc(response.result.error.message);
   });

}

飞镖计划

void handleLoadUrlResult() {

    print("Successful loaded URL");
    GoogleStorage.listTraits(
        allowInterop((var response) {
            print("Success: ${response.length}");
        }), allowInterop((var response) {
            print("Failed: ${response}");
    }));
}