skip to Main Content

I have enabled the anonymous sign in method in firebase project. and this is the code I am trying to make an anonymous login:

try {
  final userCredential = await FirebaseAuth.instance.signInAnonymously();
  print("Signed in with temporary account.");
} on FirebaseAuthException catch (e) {
  switch (e.code) {
    case "operation-not-allowed":
      print("Anonymous auth hasn't been enabled for this project.");
      break;
    default:
      print("Unknown error. ${e.code}");
  }
}

I do have added this code in my main function:

  await Firebase.initializeApp(
    name: 'test',
    options: DefaultFirebaseOptions.currentPlatform,
  ).catchError((e) {
    print('FB ERROR: $e');
  });

But still I am getting an error when I do try to sign in it gives an error:

Ignoring header X-Firebase-Locale because its value was null.
I/flutter (32729): Unknown error. admin-restricted-operation.

I have tried to search out a lot but couldn’t any thing related

2

Answers


  1. Can you change your code in main.dart page like this?

    void main() async {
      WidgetsFlutterBinding.ensureInitialized();
      await Firebase.initializeApp();
      runApp(MyApp());
    }
    

    if your problem is not solved if initializeApp() contains in multiple places can you remove them?
    For help: This is how I use this method in my projects.

    This code is my anonymous sign-in method (auth_service.dart):

    final _firebaseAuth = FirebaseAuth.instance;
    
      Future<User?> signInAnonymously() async {
        try {
          final UserCredential userCredentials =
          await _firebaseAuth.signInAnonymously();
          return userCredentials.user;
        } catch (_) {
          rethrow;
        }
      }
    

    Code of my login page (login_view.dart):

    bool _isLoading = false;
    
    Future<void> _signInAnonymously() async {
        try {
          setState(() {
            _isLoading = true;
          });
          Provider.of<AuthService>(context,listen: false).signInAnonymously();
        } on FirebaseAuthException catch (e) {
          _showErrorDialog(e.code);
        } catch (e) {
          _showErrorDialog(e.toString());
        }
        setState(() {
          _isLoading = false;
        });
      }
    

    Since I’m using the provider package here, Provider.of<AuthService>(context,listen: false).signInAnonymously(); I wrote it this way. You can also use it directly as signInAnonymously();.

    MyRaisedButton(
                      color: Colors.red,
                      child: Text("Anonymous",),
                      onPressed: _isLoading ? null : _signInAnonymously,
                    ),
    

    I hope I have helped. Enjoy your work.

    Login or Signup to reply.
    1. Confirm that you have enabled AnonymousSignIn
    2. Replace your Google-services.json file and check again.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search