Flutter GmailApi引发异常Object.noSuchMethod

时间:2018-10-19 00:33:36

标签: dart flutter gmail-api

我正在尝试创建一个应用,该应用可以读取Gmail邮件并进行处理。我能够使用GoogleAccountCredential和Gmail.Build API在Kotlin中实现这一目标。但是我试图在Flutter中做同样的事情,但是到目前为止没有成功。

我已使用https://pub.dartlang.org/packages/google_sign_in登录,然后创建一个类似于本文中提到的HTTPClient的HTTPClient:How to use Google API in flutter?

然后使用该客户端创建GmailApi:https://github.com/dart-lang/googleapis/blob/master/generated/googleapis/lib/gmail/v1.dart

这是我得到的例外:

E/flutter (21814): [ERROR:flutter/shell/common/shell.cc(181)] Dart Error: Unhandled exception:
E/flutter (21814): NoSuchMethodError: The method 'send' was called on null.
E/flutter (21814): Receiver: null
E/flutter (21814): Tried calling: send(Instance of '_RequestImpl')
E/flutter (21814): #0      Object.noSuchMethod (dart:core/runtime/libobject_patch.dart:50:5)
E/flutter (21814): #1      ApiRequester._request.simpleRequest (package:_discoveryapis_commons/src/clients.dart:213:26)
E/flutter (21814): #2      ApiRequester._request (package:_discoveryapis_commons/src/clients.dart:242:25)
E/flutter (21814): #3      ApiRequester.request (package:_discoveryapis_commons/src/clients.dart:66:26)
E/flutter (21814): <asynchronous suspension>
E/flutter (21814): #4      UsersMessagesResourceApi.list (package:googleapis/gmail/v1.dart:1540:32)
E/flutter (21814): #5      SignInDemoState._handleGetContact (package:expense_manager/ui/message_homepage.dart:57:69)
E/flutter (21814): <asynchronous suspension>
E/flutter (21814): #6      SignInDemoState.initState.<anonymous closure> (package:expense_manager/ui/message_homepage.dart:42:9)
E/flutter (21814): #7      _RootZone.runUnaryGuarded (dart:async/zone.dart:1314:10)
E/flutter (21814): #8      _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:336:11)
E/flutter (21814): #9      _DelayedData.perform (dart:async/stream_impl.dart:591:14)
E/flutter (21814): #10     _StreamImplEvents.handleNext (dart:async/stream_impl.dart:707:11)
E/flutter (21814): #11     _PendingEvents.schedule.<anonymous closure> (dart:async/stream_impl.dart:667:7)
E/flutter (21814): #12     _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
E/flutter (21814): #13     _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)
I/flutter (21814): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY 

这是课程:

class GoogleHttpClient extends IOClient {
Map<String, String> _headers;

GoogleHttpClient(this._headers) : super();

@override
Future<StreamedResponse> send(BaseRequest request) =>
    super.send(request..headers.addAll(_headers));

@override
Future<Response> head(Object url, {Map<String, String> headers}) =>
    super.head(url, headers: headers..addAll(_headers));
}

这样调用:

_googleSignIn.signIn().then((data) {
  data.authHeaders.then((result) {
    _client = new GoogleHttpClient(result);
  });
});
gmail.GmailApi gmailApi = gmail.GmailApi(_client);
gmail.ListMessagesResponse resp = await gmailApi.users.messages.list(
    'me', q: 'from:bankofamerica');

1 个答案:

答案 0 :(得分:1)

欢迎使用StackOverflow!

您的代码错误地处理了Dart的Future类:

您正在尝试访问_client变量,但是它可能尚未初始化。

两种处理方式:

异步/等待

void _signInAsync() async {
  final data = await _googleSignIn.signIn();
  final result = await data.authHeaders;

  _client = new GoogleHttpClient(result);

  gmail.GmailApi gmailApi = gmail.GmailApi(_client);
  gmail.ListMessagesResponse resp = await gmailApi.users.messages.list(
      'me', q: 'from:bankofamerica');
}

未来。

void _signInThen() {
  _googleSignIn.signIn().then((data) {
    data.authHeaders.then((result) {
      _client = new GoogleHttpClient(result);
      gmail.GmailApi gmailApi = gmail.GmailApi(_client);
      gmail.ListMessagesResponse resp = await gmailApi.users.messages.list(
          'me', q: 'from:bankofamerica');
    });
  });
}

高度建议阅读Dart文档中的以下页面:https://www.dartlang.org/tutorials/language/futures