Flutter GmailApi引发异常Object.noSuchMethod [英] Flutter GmailApi throws exception Object.noSuchMethod

查看:43
本文介绍了Flutter GmailApi引发异常Object.noSuchMethod的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to create a app which reads messages from Gmail and process them. I was able to achieve that in kotlin using GoogleAccountCredential and Gmail.Build APIs. But I am trying to do the same in Flutter, but no success so far.

我已经使用 https://pub.dartlang.org/packages/google_sign_in 来登录,然后创建一个类似于本文中提到的HTTPClient:如何在扑朔迷离中使用Google API?

I have used https://pub.dartlang.org/packages/google_sign_in to sign in, and then create a HTTPClient similar to one mentioned in this post: How to use Google API in flutter?

然后使用该客户端创建GmailApi:

Then used that client to create 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');

推荐答案

欢迎使用StackOverflow!

Welcome to StackOverflow!

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

Your code handles Dart's Future classes incorrectly:

您正在尝试访问 _client 变量,但该变量可能尚未初始化.

You are trying to access the _client variable, but it may not have been initialized yet.

两种处理方式:

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

I highly recommend reading this page in the Dart documentation: https://www.dartlang.org/tutorials/language/futures

这篇关于Flutter GmailApi引发异常Object.noSuchMethod的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆