简体   繁体   English

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

[英]js-interop: How to access the input data in the callback function?

I've written a wrapper class to access Google Sheet using the javascripts API (as Dart version of Google API is not working). 我已经编写了一个包装类,以使用javascripts API访问Google表格(因为Dart版本的Google API无法正常工作)。 In the callback function, I can see the data are stored in the body and results field in the debugger on Dartium but could not access them from Dart. 在回调函数中,我可以看到数据存储在Dartium上调试器的bodyresults字段中,但无法从Dart访问它们。 Any suggestion would be much appreciated. 任何建议将不胜感激。 The program is using Dart 1.18. 该程序使用的是Dart 1.18。

在此处输入图片说明 在此处输入图片说明

Javascript Code 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);
}

Dart Program 飞镖计划

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}");
    }));
}

Finally found the solution. 终于找到了解决方案。 The problem is apparently caused by allowInterop() not working with js response that contains non supported data type. 该问题显然是由于allowInterop()无法与包含不支持的数据类型的js response一起使用。 By returning just the array list of data, it works perfectly. 通过仅返回数据的数组列表,它可以完美地工作。 The js list is correctly mapped over to Dart's list by allowInterop() . js列表通过allowInterop()正确映射到Dart的列表。

Javascript Code 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);
   });

}

Dart Program 飞镖计划

void handleLoadUrlResult() {

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM