0

So I'm trying to create a simple Sign In page in Flutter using Google Firebase but Firebase is not giving me desired output. Maybe its error in Asynchronous Function cause its giving me output Instance of 'Future<dynamic>'

Code for Anonymously Sign In dart file:

import 'package:firebase_auth/firebase_auth.dart';

class AuthService {
  final FirebaseAuth _auth = FirebaseAuth.instance;

  Future signAnonymous() async {
    try {
      AuthResult result = await _auth.signInAnonymously();
      FirebaseUser user = result.user;
      return user; 
    } catch (e) {
      print(e.toString());
      return null;
    }
  }
}

Sign In Page Code:

import 'package:flutter/material.dart';
import 'package:freezeria/Screens/services/auth.dart';

class signin extends StatefulWidget {
  @override
  _signinState createState() => _signinState();
}

class _signinState extends State<signin> {
  final AuthService _auth = AuthService();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.purple[300],
      appBar: AppBar(
        backgroundColor: Colors.purple[600],
        elevation: 0.0,
        centerTitle: true,
        title: Text('Sign In To Freezeria!'),
      ),
        body: Container(
          padding: EdgeInsets.symmetric(vertical: 20.0, horizontal: 50.0),
          child: ElevatedButton(
            child: Text('SignIn'),
            style: ButtonStyle(
              backgroundColor:
                  MaterialStateProperty.all<Color>(Colors.purple[100]),
            ),
            onPressed: () async {
              dynamic result = _auth.signAnonymous();
              if (result == null) {
                print("Error Signing In");
              } else {
                print('Signed In');
                print(result);
              }
            }
          )
       ),
    );
  }
}

The Output I got was:

I/flutter ( 8533): Signed In
I/flutter ( 8533): Instance of 'Future<dynamic>'

I need to get user details provided by the firebase

Any help will be appreciated :)

1 Answer 1

1

_auth.signAnonymous() is an async function, for that matter it returns a Future ! Use the await keyword to get the user value instead of a Future object !

result = await _auth.signAnonymous();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.