Home > Software design >  The getter 'user' isn't defined for the type 'Future<UserCredential>'
The getter 'user' isn't defined for the type 'Future<UserCredential>'

Time:12-28

I'm taking a course for Flutter and I think the course is a bit old, I'm getting an error like this, on this line;

@override
  Future<auth.User?> logInWithEmailAndPassword(
      {@required String? email,
        @required String? sifre,
      }) async {
    try{
      final credential = _firebaseAuth.signInWithEmailAndPassword(
        email: email!,
        password: sifre!,
      );
      return credential.user;
    } on auth.FirebaseAuthException catch (err) {
      throw Failure(code: err.code, message: err.message);
    } on PlatformException catch (err) {
      throw Failure(code: err.code, message: err.message);
    }
  }

return credential.user;

The getter 'user' isn't defined for the type 'Future'. (Documentation) Try importing the library that defines 'user', correcting the name to the name of an existing getter, or defining a getter or field named 'user'.

There is no such problem in the video. how can I solve it?

import 'package:basla/config/paths.dart';
import 'package:basla/depolar/depolar.dart';
import 'package:basla/models/models.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart' as auth;
import 'package:flutter/services.dart';
import 'package:meta/meta.dart';

class AuthRepository extends BaseAuthRepository {
  late final FirebaseFirestore _firebaseFirestore;
  late final auth.FirebaseAuth _firebaseAuth;

  AuthRepository({
    FirebaseFirestore? firebaseFirestore,
    auth.FirebaseAuth? firebaseAuth,
  })  : _firebaseFirestore = firebaseFirestore ?? FirebaseFirestore.instance,
        _firebaseAuth = firebaseAuth ?? auth.FirebaseAuth.instance;

  @override
  Stream<auth.User?> get user => _firebaseAuth.userChanges();

  @override
  Future<auth.User?> signUpWithEmailAndPassword(
      {@required String? kullaniciadi,
      @required String? email,
      @required String? sifre}) async {
    try {
      final credential = await _firebaseAuth.createUserWithEmailAndPassword(
        email: email!,
        password: sifre!,
      );
      final user = credential.user;
      _firebaseFirestore.collection(Paths.users).doc(user?.uid).set({
        'Kullanıcı Adı': kullaniciadi,
        'Email': email,
        'Takipci': 0,
        'Takip ': 0
      });
      return user;
    } on auth.FirebaseAuthException catch (err) {
      throw Failure(code: err.code, message: err.message);
    } on PlatformException catch (err) {
      throw Failure(code: err.code, message: err.message);
    }
  }

  @override
  Future<auth.User?> logInWithEmailAndPassword(
      {@required String? email,
        @required String? sifre,
      }) async {
    try{
      final credential = _firebaseAuth.signInWithEmailAndPassword(
        email: email!,
        password: sifre!,
      );
      return credential.user;
    } on auth.FirebaseAuthException catch (err) {
      throw Failure(code: err.code, message: err.message);
    } on PlatformException catch (err) {
      throw Failure(code: err.code, message: err.message);
    }
  }

  @override
  Future<void> logOut() async {
    await _firebaseAuth.signOut();
  }
}

CodePudding user response:

Change final credential = _firebaseAuth.signInWithEmailAndPassword( to final credential = await _firebaseAuth.signInWithEmailAndPassword(.

You have to wait for signInWithEmailAndPassword to complete before returning credential.user.

  • Related