Phyln
Phyln

Reputation: 81

Error, while adding authentication providers to Firebase_ui_auth package in Flutter

I am using firebase_ui_auth as user authentication in a Flutter app. When implementing providers, according to the [GitHub instructions][1], I get this error The argument type 'List<Object>' can't be assigned to the parameter type 'List<AuthProvider<AuthListener, AuthCredential>>?

When I use only var providers = [EmailAuthProvider()], it goes fine but when I add var providers = [EmailAuthProvider(), GoogleAuthProvider()], it throws the error.

Documentation from GitHub repository (by Firebase) says that I can add more providers to the providers list as screenshot below.

[![enter image description here][2]][2]

Please, how do I solve this?

My main.dart code below

  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  FirebaseUIAuth.configureProviders([
    EmailAuthProvider(),
    GoogleAuthProvider() //<-Error here when added GoogleAuthProvider()
  ]);
  SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'My Cool App',
      theme: ThemeData(primarySwatch: Colors.indigo),
      themeMode: ThemeMode.light,
      home: const AuthGate(),
      builder: EasyLoading.init(),
    );
  }
}

class AuthGate extends StatelessWidget {
  const AuthGate({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    var providers = [EmailAuthProvider(), GoogleAuthProvider()]; //<-Error here when added GoogleAuthProvider()
    return StreamBuilder(
      stream: FirebaseAuth.instance.authStateChanges(),
      builder: (context, snapshot) {
        if (snapshot.hasData) {
          return const MyHomePage();
        } else {
          return SignInScreen(
            providers: providers,
          );
        }
      },
    );
  }
}```

Seems like the link and image aren't displaying well.

  [1]: https://github.com/firebase/flutterfire/blob/master/packages/firebase_ui_auth/doc/providers/email.md
  [2]: https://i.sstatic.net/PlOTQ.png

Upvotes: 2

Views: 773

Answers (3)

Antonio Cappiello
Antonio Cappiello

Reputation: 514

First check your imports, make sure to have the "hide" part:

import 'package:firebase_auth/firebase_auth.dart' hide EmailAuthProvider;
import 'package:firebase_ui_auth/firebase_ui_auth.dart';
import 'package:firebase_ui_oauth_google/firebase_ui_oauth_google.dart';

Then build the providers like this:

providers: [
             EmailAuthProvider(),
             GoogleProvider(clientId: "YOUR_WEBCLIENT_ID"),  // new
           ],

For the full source see this official guide: https://firebase.google.com/codelabs/firebase-auth-in-flutter-apps#6

Upvotes: -1

Roger
Roger

Reputation: 61

You need to add your 'GOOGLE_CLIENT_ID' to GoogleProvider.

GoogleProvider(clientId: GOOGLE_CLIENT_ID)

More info here: https://github.com/firebase/FirebaseUI-Flutter/blob/main/docs/firebase-ui-auth/providers/oauth.md#google-sign-in

Upvotes: -1

Vivek
Vivek

Reputation: 494

Looks like your code is mixed up with the previous deprecated package FlutterFire UI. When you use Firebase UI Auth, you need to use firebase_ui_oauth_XXXX package. So, for google sign in, you need to add Firebase UI OAuth Google package.

Your code should be adjusted as below.

FirebaseUIAuth.configureProviders([
EmailAuthProvider(),
GoogleProvider(),]);

P.S. Both firebase_auth and firebase_ui_auth package have EmailAuthProvider methods. So, make sure you import firebase_auth as below when both of these packages are imported together.

import 'package:firebase_auth/firebase_auth.dart' hide EmailAuthProvider;

Upvotes: 0

Related Questions