google_sign_in 0.0.1

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 50

google_sign_in #

A Flutter plugin for Google Sign In.

Note: This is a work-in-progress, and is not fully supported by the Flutter team. (For example, we don't have this under continuous integration and testing.)

Android integration #

To access Google Sign-In, you'll need to make sure to register your application.

You don't need to include the google-services.json file in your app unless you are using Google services that require it. You do need to enable the OAuth APIs that you want, using the Google Cloud Platform API manager. For example, if you want to mimic the behavior of the Google Sign-In sample app, you'll need to enable the Google People API.

iOS integration #

To access Google Sign-In, you'll need to make sure to register your application. Add the generated GoogleService-Info.plist to root of your Runner project in Xcode, so that the Google Sign-In framework can determine your client id.

You'll need to add this to the main dictionary of your application's Info.plist:

        <key>CFBundleURLTypes</key>
        <array>
                <dict>
                        <key>CFBundleTypeRole</key>
                        <string>Editor</string>
                        <key>CFBundleURLSchemes</key>
                        <array>
                                <!-- bundle id, for example: -->
                                <string>com.yourcompany.myapp</string>
                        </array>
                </dict>
                <dict>
                        <key>CFBundleTypeRole</key>
                        <string>Editor</string>
                        <key>CFBundleURLSchemes</key>
                        <array>
                                <!-- reverse url of your client id, for example: -->
        <string>com.googleusercontent.apps.861823949799-11qfr04mrfh2mndp3el2vgc0e357a2t6</string>
                        </array>
                </dict>
        </array>

Usage #

Add the following import to your Dart code:

import 'package:google_sign_in/google_sign_in.dart';

Initialize GoogleSignIn with the scopes you want:

GoogleSignIn.initialize(
    scopes: [
      'email',
      'https://www.googleapis.com/auth/contacts.readonly',
    ],
);

You can now use the GoogleSignIn class to authenticate in your Dart code, e.g.

GoogleSignInAccount account = await (await GoogleSignIn.instance).signIn();

See google_sign_in.dart for more API details.

Issues and feedback #

Please file issues to send feedback or report a bug. Thank you!

0.0.1 #

  • Initial Release

example/lib/main.dart

import 'dart:async';
import 'dart:convert' show JSON;

import "package:http/http.dart" as http;
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:google_sign_in/google_sign_in.dart';

GoogleSignIn _googleSignIn = new GoogleSignIn(
  scopes: [
    'email',
    'https://www.googleapis.com/auth/contacts.readonly',
  ],
);

void main() {
  runApp(
      new MaterialApp(
          title: 'Google Sign In',
          home: new SignInDemo(),
      ),
  );
}

class SignInDemo extends StatefulWidget {
  @override
  State createState() => new SignInDemoState();
}

class SignInDemoState extends State<SignInDemo> {
  GoogleSignInAccount _currentUser;
  String _contactText;

  @override
  void initState() {
    super.initState();
    _googleSignIn.onCurrentUserChanged.listen((GoogleSignInAccount account) {
      setState(() {
        _currentUser = account;
      });
      if (_currentUser != null) {
        _handleGetContact();
      }
    });
    _googleSignIn.signInSilently();
  }

  Future<Null> _handleGetContact() async {
    setState(() {
      _contactText = "Loading contact info...";
    });
    http.Response response = await http.get(
        'https://people.googleapis.com/v1/people/me/connections',
        headers: await _currentUser.authHeaders,
    );
    if (response.statusCode != 200) {
      setState(() {
        _contactText = "People API gave a ${response.statusCode} " +
            "response. Check logs for details.";
      });
      print('People API ${response.statusCode} response: ${response.body}');
      return;
    }
    Map<String, dynamic> data = JSON.decode(response.body);
    String namedContact = _pickFirstNamedContact(data);
    setState(() {
      if (namedContact != null) {
        _contactText = "I see you know $namedContact!";
      } else {
        _contactText = "No contacts to display.";
      }
    });
  }

  String _pickFirstNamedContact(Map<String, dynamic> data) {
    List<Map<String, dynamic>> connections = data['connections'];
    Map<String, dynamic> contact = connections?.firstWhere(
            (Map<String, dynamic> contact) => contact['names'] != null,
        orElse: () => null,
    );
    if (contact != null) {
      Map<String, dynamic> name = contact['names'].firstWhere(
              (Map<String, dynamic> name) => name['displayName'] != null,
          orElse: () => null,
      );
      if (name != null) {
        return name['displayName'];
      }
    }
    return null;
  }

  Future<Null> _handleSignIn() async {
    try {
      await _googleSignIn.signIn();
    } catch (error) {
      print(error);
    }
  }

  Future<Null> _handleSignOut() async {
    _googleSignIn.disconnect();
  }

  Widget _buildBody() {
    if (_currentUser != null) {
      return new Column(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: <Widget>[
            new ListTile(
                leading: new GoogleUserCircleAvatar(_currentUser.photoUrl),
                title: new Text(_currentUser.displayName),
                subtitle: new Text(_currentUser.email),
            ),
            new Text("Signed in successfully."),
            new Text(_contactText),
            new RaisedButton(
                child: new Text('SIGN OUT'),
                onPressed: _handleSignOut,
            ),
            new RaisedButton(
                child: new Text('REFRESH'),
                onPressed: _handleGetContact,
            ),
          ],
      );
    } else {
      return new Column(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: <Widget>[
            new Text("You are not currently signed in."),
            new RaisedButton(
                child: new Text('SIGN IN'),
                onPressed: _handleSignIn,
            ),
          ],
      );
    }
  }

  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: new AppBar(
            title: new Text('Google Sign In'),
        ),
        body: new ConstrainedBox(
            constraints: const BoxConstraints.expand(),
            child: _buildBody(),
        ));
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  google_sign_in: ^0.0.1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:google_sign_in/google_sign_in.dart';
  
Version Uploaded Documentation Archive
4.0.1+3 Apr 4, 2019 Go to the documentation of google_sign_in 4.0.1+3 Download google_sign_in 4.0.1+3 archive
4.0.1+1 Feb 15, 2019 Go to the documentation of google_sign_in 4.0.1+1 Download google_sign_in 4.0.1+1 archive
4.0.1 Feb 8, 2019 Go to the documentation of google_sign_in 4.0.1 Download google_sign_in 4.0.1 archive
4.0.0+1 Feb 8, 2019 Go to the documentation of google_sign_in 4.0.0+1 Download google_sign_in 4.0.0+1 archive
4.0.0 Jan 28, 2019 Go to the documentation of google_sign_in 4.0.0 Download google_sign_in 4.0.0 archive
3.3.0+1 Jan 28, 2019 Go to the documentation of google_sign_in 3.3.0+1 Download google_sign_in 3.3.0+1 archive
3.3.0 Jan 24, 2019 Go to the documentation of google_sign_in 3.3.0 Download google_sign_in 3.3.0 archive
3.2.4 Nov 10, 2018 Go to the documentation of google_sign_in 3.2.4 Download google_sign_in 3.2.4 archive
3.2.2 Oct 24, 2018 Go to the documentation of google_sign_in 3.2.2 Download google_sign_in 3.2.2 archive
3.2.1 Oct 3, 2018 Go to the documentation of google_sign_in 3.2.1 Download google_sign_in 3.2.1 archive

All 34 versions...

Popularity:
Describes how popular the package is relative to other packages. [more]
100
Health:
Code health derived from static analysis. [more]
--
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
--
Overall:
Weighted score of the above. [more]
50
Learn more about scoring.

This package version is not analyzed, because it is more than two years old. Check the latest stable version for its analysis.

The package version is not analyzed, because it does not support Dart 2. Until this is resolved, the package will receive a health and maintenance score of 0.

Analysis issues and suggestions

Support Dart 2 in pubspec.yaml.

The SDK constraint in pubspec.yaml doesn't allow the Dart 2.0.0 release. For information about upgrading it to be Dart 2 compatible, please see https://dart.dev/dart-2#migration.

Maintenance issues and suggestions

Make sure dartdoc successfully runs on your package's source files. (-10 points)

Dependencies were not resolved.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.8.0 <2.0.0