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 :)


Solution 1: Gaspard Merten

_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();