0
votes

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.

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?

Then used that client to create GmailApi : https://github.com/dart-lang/googleapis/blob/master/generated/googleapis/lib/gmail/v1.dart

Here is the exception I get:

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 

Here is the class:

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

Invoked like this:

_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
As mentioned in your error code, seems like the method 'send' was called on null. Please provide the related code in your question, don't expect others to guess what code you used from all those linksbunbun
@bunbun, added code in the description.Manoj Kumar

1 Answers

2
votes

Welcome to StackOverflow!

Your code handles Dart's Future classes incorrectly:

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

Two ways of handling this:

async/await

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

Future.then

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

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