Flutter 3.41 is live! Check out theFlutter 3.41 blog post!
Google APIs
How to use Google APIs with Flutter.
TheGoogle APIs package exposes dozens of Google services that you can use from Dart projects.
This page describes how to use APIs that interact with end-user data by using Google authentication.
Examples of user-data APIs includeCalendar,Gmail,YouTube, and Firebase.
The only APIs you should use directly from your Flutter project are those that access user data using Google authentication.
APIs that requireservice accountsshould not be used directly from a Flutter application. Doing so requires shipping service credentials as part of your application, which is not secure. To use these APIs, we recommend creating an intermediate service.
To add authentication to Firebase explicitly, check out theAdd a user authentication flow to a Flutter app using FirebaseUI codelab and theGet Started with Firebase Authentication on Flutter docs.
Overview
#To use Google APIs, follow these steps:
- Pick the desired API
- Enable the API
- Authenticate and determine the current user
- Obtain an authenticated HTTP client
- Create and use the desired API class
1. Pick the desired API
# The documentation forpackage:googleapis lists each API as a separate Dart library&emdash;in aname_version format. Check outyoutube_v3 as an example.
Each library might provide many types, but there is oneroot class that ends inApi. For YouTube, it'sYouTubeApi.
Not only is theApi class the one you need to instantiate (see step 3), but it also exposes the scopes that represent the permissions needed to use the API. For example, theConstants section of theYouTubeApi class lists the available scopes. To request access to read (but not write) an end-user's YouTube data, authenticate the user withyoutubeReadonlyScope.
/// Provides the `YouTubeApi` class.import'package:googleapis/youtube/v3.dart';2. Enable the API
#To use Google APIs you must have a Google account and a Google project. You also need to enable your desired API.
This example enablesYouTube Data API v3. For details, see thegetting started instructions.
3. Authenticate and determine the current user
#Use thegoogle_sign_in package to authenticate users with their Google identity. Configure sign in for each platform you want to support.
/// Provides the `GoogleSignIn` class.import'package:google_sign_in/google_sign_in.dart'; The package's functionality is accessed through a static instance of theGoogleSignIn class. Before interacting with the instance, theinitialize method must be called and allowed to complete.
final_googleSignIn=GoogleSignIn.instance;@overridevoidinitState(){super.initState();_googleSignIn.initialize();// ···}Once initialization is complete but before user authentication, listen to authentication events to determine if a user signed in.
GoogleSignInAccount?_currentUser;@overridevoidinitState(){super.initState();_googleSignIn.initialize().then((_){_googleSignIn.authenticationEvents.listen((event){setState((){_currentUser=switch(event){GoogleSignInAuthenticationEventSignIn()=>event.user,_=>null,};});});});}Once you're listening to any relevant authentication events, you can attempt to authenticate a previously signed-in user.
voidinitState(){super.initState();_googleSignIn.initialize().then((_){// ...// Attempt to authenticate a previously signed in user._googleSignIn.attemptLightweightAuthentication();});} To also allow for new users to authenticate, follow the instructions provided bypackage:google_sign_in.
Once a user has been authenticated, you must obtain an authenticated HTTP client.
4. Obtain an authenticated HTTP client
# Once you have a signed-in user, request the relevant client authorization tokens usingauthorizationForScopes for the API scopes that your app requires.
constrelevantScopes=[YouTubeApi.youtubeReadonlyScope];finalauthorization=awaitcurrentUser.authorizationClient.authorizationForScopes(relevantScopes); If your scopes require user interaction, you'll need to useauthorizeScopes from an interaction handler instead ofauthorizationForScopes.
Once you have the relevant authorization tokens, use theauthClient extension frompackage:extension_google_sign_in_as_googleapis_auth to set up an authenticated HTTP client with the relevant credentials applied.
import'package:extension_google_sign_in_as_googleapis_auth/extension_google_sign_in_as_googleapis_auth.dart';finalauthenticatedClient=authorization!.authClient(scopes:relevantScopes,);5. Create and use the desired API class
#Use the API to create the desired API type and call methods. For instance:
finalyouTubeApi=YouTubeApi(authenticatedClient);finalfavorites=awaityouTubeApi.playlistItems.list(['snippet'],playlistId:'LL',// Liked List);More information
#You might want to check out the following:
- The
extension_google_sign_in_as_googleapis_authexample is a working implementation of the concepts described on this page.
Unless stated otherwise, the documentation on this site reflects Flutter 3.38.6. Page last updated on 2025-10-30.View source orreport an issue.