Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4133 pages
Flutter and app store releases

Flutter code written for google_sign_in 6 stops compiling after the upgrade to 7

Your error

error • The class 'GoogleSignIn' doesn't have an unnamed constructor. Try using one of the named constructors defined in 'GoogleSignIn' • new_with_undefined_constructor_default3 more lines it can show

Fix it fast

Most common cause, 1 of 3

The code creates its own GoogleSignIn object

Use GoogleSignIn.instance and call initialize at startup.

Fix
import 'package:google_sign_in/google_sign_in.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await GoogleSignIn.instance.initialize(
    serverClientId: 'YOUR_WEB_CLIENT_ID.apps.googleusercontent.com',
  );
  runApp(const MyApp());
}
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

google_sign_in 7 is a rewrite. GoogleSignIn is now a singleton reached through GoogleSignIn.instance, initialize() must be called once first, signIn() became authenticate(), signInSilently() became attemptLightweightAuthentication(), and access tokens come from authorizationClient instead of authentication. Update each call, or pin ^6 until you can migrate.

  • Applies to google_sign_in 7.2.0, Flutter 3.44, Dart 3.12
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Confirm the resolved version, then list every call site. Each old call has one replacement, so the list is your migration checklist.

Run this first
flutter pub deps --style=compact | grep google_sign_in
grep -rnE "GoogleSignIn\(|\.signIn\(|signInSilently|\.authentication|accessToken" lib

Then find the line you see in this list and open its fix:

  1. 1The code creates its own GoogleSignIn objectdoesn't have an unnamed constructor, at a line like GoogleSignIn(scopes: [...])
  2. 2signIn and signInSilently were renamed and splitsignIn or signInSilently isn't defined for the type 'GoogleSignIn'
  3. 3accessToken is read from authenticationThe getter 'accessToken' isn't defined for the type 'GoogleSignInAuthentication'

What does the error look like?

  • error • The class 'GoogleSignIn' doesn't have an unnamed constructor. Try using one of the named constructors defined in 'GoogleSignIn' • new_with_undefined_constructor_default
  • error • The method 'signIn' isn't defined for the type 'GoogleSignIn'.
  • error • The method 'signInSilently' isn't defined for the type 'GoogleSignIn'.
  • error • The getter 'accessToken' isn't defined for the type 'GoogleSignInAuthentication'.

Cause 1 of 3

The code creates its own GoogleSignIn object

You seedoesn't have an unnamed constructor, at a line like GoogleSignIn(scopes: [...])

Check

The package's migration notes say GoogleSignIn is a singleton in 7 and that an explicit initialize step must run exactly once before any other method. Scopes are no longer passed to a constructor.

Check
grep -rn "GoogleSignIn(" lib

Fix

Use GoogleSignIn.instance and call initialize at startup. On Android, a project without google-services.json passes the web application client ID as serverClientId.

Fix
import 'package:google_sign_in/google_sign_in.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await GoogleSignIn.instance.initialize(
    serverClientId: 'YOUR_WEB_CLIENT_ID.apps.googleusercontent.com',
  );
  runApp(const MyApp());
}

Verify

flutter analyze no longer reports the constructor error.

Verify
flutter analyze lib

Confirmed by github.com

Did this fix it?

Cause 2 of 3

signIn and signInSilently were renamed and split

You seesignIn or signInSilently isn't defined for the type 'GoogleSignIn'

Check

In 7, authenticate() handles the interactive sign-in and attemptLightweightAuthentication() replaces the silent one, which may now show a small UI. Web does not support authenticate(); supportsAuthenticate() tells you at runtime.

Check
grep -rnE "\.signIn\(|signInSilently" lib

Fix

Call the new methods. Pass scopes you will need soon as scopeHint so supported platforms can ask for them in the same screen.

Fix
final signIn = GoogleSignIn.instance;

// app start: restore a previous session if possible
final restored = await signIn.attemptLightweightAuthentication();

// sign-in button (not on web, which uses renderButton)
if (signIn.supportsAuthenticate()) {
  final account = await signIn.authenticate(scopeHint: ['email']);
}

Verify

Run on a device and sign in; the account email prints and analyze is clean.

Verify
flutter run

Confirmed by github.com

Did this fix it?

Cause 3 of 3

accessToken is read from authentication

You seeThe getter 'accessToken' isn't defined for the type 'GoogleSignInAuthentication'

Check

Authentication (who the user is) and authorization (which scopes the app may use) are separate in 7. account.authentication still gives the idToken; the access token now comes from authorizationClient.

Check
grep -rn "accessToken" lib

Fix

Ask for an existing grant first and only show the consent UI when there is none.

Fix
const scopes = ['https://www.googleapis.com/auth/drive.readonly'];

final idToken = account.authentication.idToken;
final authz =
    await account.authorizationClient.authorizationForScopes(scopes) ??
    await account.authorizationClient.authorizeScopes(scopes);
final accessToken = authz.accessToken;

Verify

The access token is non-null after the user grants the scopes, and API calls with it succeed.

Verify
flutter analyze lib

Confirmed by pub.dev

Did this fix it?

How do I stop it coming back?

  • Pin plugins with a caret on the major version (google_sign_in: ^6.2.0) so pub upgrade never crosses a major release by surprise.
  • Read a plugin's MIGRATION.md before raising its major version.
  • Keep sign-in code in one service class so an API rewrite touches one file.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.