调用clientViaServiceAccount

时间:2019-03-09 19:26:29

标签: google-sheets dart flutter google-sheets-api

我正在尝试将用户的名字和姓氏添加到目标Google工作表的单元格A和B中,但我一直收到错误消息:

  

[错误:flutter / lib / ui / ui_dart_state.cc(148)]未处理的异常:类型   在强制转换类型中,“字符串”不是“列表”类型的子类型

每当我提交我的代码时。

我知道错误被隔离到 clientViaServiceAccount 代码块中,因为这是错误堆栈指向的代码行。我尝试在参数中将_scopes更改为[SheetsApi.SpreadsheetsScope],但这导致了同样的事情。

我根据此处的示例对该代码块进行了建模: https://pub.dartlang.org/packages/googleapis#-example-tab- 第二个答案在这里: Google Sheets API v4 for Flutter/Dart

if (_formKey.currentState.validate()){
  Navigator.push(
    context, 
    MaterialPageRoute(builder: (context) => ConfirmationPage()),
  );
  _formKey.currentState.save();
  final _privateKey = { 'private JSON key' };
  final _cred = new ServiceAccountCredentials.fromJson(_privateKey);
  final _scopes = [SheetsApi.SpreadsheetsScope];
  Map<String,String> _sheets = {
    "ISMI": 'sheet key',
    "NORCO": 'sheet key 2'
  };
  clientViaServiceAccount(_cred, _scopes).then((client){
    var api = new SheetsApi(client);
    ValueRange vr = new ValueRange.fromJson({"values": [_newUser.firstName,_newUser.lastName]});
    api.spreadsheets.values.append(vr,_sheets[_newUser.location],'A:B').then((AppendValuesResponse r) {
      client.close();
    });
  });
}

任何帮助将不胜感激,

谢谢

1 个答案:

答案 0 :(得分:0)

问题在于JSON必须被r''''''包围。因此,我将_privateKey变量更改为此:

final _privateKey = r'''{private JSON key}''';
相关问题