0

I'm fumbling with my loginPage.dart. I upgraded the pre- 1.12 project. This seems to be the last part of the migration

I made the errors bold within the code block

The Errors are:

‘User’ isn’t a function Try correcting the name to match an existing function, or define a method or function named ‘User’

Error:

The getter ‘uid’, isn’t defined for the type ‘UserCredential’ Try importing the library that defines ‘did’, correcting the name from all but one of the imports

Error:

The name 'User’ isn’t defined in the libraries 'package:firebase_auth/firebase_auth.dar Try correcting the name to match an existing function, or define a method or function named ‘User’

Hopefully some one can help me out?

From firebase_core: ^0.4.0+1 to firebase_core: ^0.5.0
From firebase_auth: ^0.11.1+3 to firebase_auth: ^0.18.0+1
From cloud_firestore: ^0.12.7+1 to cloud_firestore: ^0.14.0+2
From firebase_storage: ^3.0.4 to firebase_storage: ^4.0.0

loginPage.dart

import 'package:firebase_auth/firebase_auth.dart';

import '../Models/appConstants.dart';
import '../Models/userObjects.dart';
import './guestHomePage.dart';
import './signUpPage.dart';

class LoginPage extends StatefulWidget {
  static final String routeName = '/loginPageRoute';

  LoginPage({Key key}) : super(key: key);

  @override
  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  final _formKey = GlobalKey<FormState>();
  TextEditingController _emailController = TextEditingController();
  TextEditingController _passwordController = TextEditingController();

  void _signUp() {
    if (_formKey.currentState.validate()) {
      String email = _emailController.text;
      String password = _passwordController.text;
      **AppConstants.currentUser = User();**
      AppConstants.currentUser.email = email;
      AppConstants.currentUser.password = password;
      Navigator.pushNamed(context, SignUpPage.routeName);
    }
  }

  void _login() {
    if (_formKey.currentState.validate()) {
      String email = _emailController.text;
      String password = _passwordController.text;
      FirebaseAuth.instance
          .signInWithEmailAndPassword(
        email: email,
        password: password,
      )
          .then((firebaseUser) {
        **String userID = firebaseUser.uid;**
        **AppConstants.currentUser = User(id: userID);**
        AppConstants.currentUser
            .getPersonalInfoFromFirestore()
            .whenComplete(() {
          Navigator.pushNamed(context, GuestHomePage.routeName);
        });
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SingleChildScrollView(
        child: Center(
          child: Padding(
            padding: const EdgeInsets.fromLTRB(50, 100, 50, 0),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              children: <Widget>[
                Text(
                  'Welcome to ${AppConstants.appName}!',
                  style: TextStyle(
                    fontWeight: FontWeight.bold,
                    fontSize: 30.0,
                  ),
                  textAlign: TextAlign.center,
                ),
                Form(
                  key: _formKey,
                  child: Column(
                    children: <Widget>[
                      Padding(
                        padding: const EdgeInsets.only(top: 35.0),
                        child: TextFormField(
                          decoration: InputDecoration(labelText: 'Email'),
                          style: TextStyle(
                            fontSize: 25.0,
                          ),
                          validator: (text) {
                            if (!text.contains('@')) {
                              return 'Please enter a valid email';
                            }
                            return null;
                          },
                          controller: _emailController,
                        ),
                      ),
                      Padding(
                        padding: const EdgeInsets.only(top: 25.0),
                        child: TextFormField(
                          decoration: InputDecoration(labelText: 'Password'),
                          style: TextStyle(
                            fontSize: 25.0,
                          ),
                          obscureText: true,
                          validator: (text) {
                            if (text.length < 6) {
                              return 'Password must be at least 6 characters';
                            }
                            return null;
                          },
                          controller: _passwordController,
                        ),
                      )
                    ],
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.only(top: 30.0),
                  child: MaterialButton(
                    onPressed: () {
                      _login();
                    },
                    child: Text(
                      'Login',
                      style: TextStyle(
                        fontWeight: FontWeight.bold,
                        fontSize: 25.0,
                      ),
                    ),
                    color: Colors.blue,
                    height: MediaQuery.of(context).size.height / 12,
                    minWidth: double.infinity,
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(10),
                    ),
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.only(top: 30.0),
                  child: MaterialButton(
                    onPressed: () {
                      _signUp();
                    },
                    child: Text(
                      'Sign Up',
                      style: TextStyle(
                        fontWeight: FontWeight.bold,
                        fontSize: 25.0,
                      ),
                    ),
                    color: Colors.grey,
                    height: MediaQuery.of(context).size.height / 12,
                    minWidth: double.infinity,
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(10),
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
} ```
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
RobB
  • 383
  • 1
  • 2
  • 12

2 Answers2

1

The FlutterFire library was recently upgraded, which is probably where your problems come from.

  1. The FirebaseUser class is now simply called User, which you should also see if you hover over the error in your IDE.

  2. To get the current user, you no longer do await FirebaseAuth.instance.currentUser(), but instead do FirebaseAuth.instance.currentUser (so without await and without ()).

  3. The signInWithEmailAndPassword method resolves to an UserCredential object. To get the UID, you do:

    .then((credentials) {
        String userID = credentials.user.uid;
    
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Hi Frank, Thank you for your input! This part seems to be fixed! in the _signUp there is still an error on: AppConstants.currentUser = User(); And similar in the _login() AppConstants.currentUser = User(id: userID); Hopefully you have a solution? Where can I find documentation on this? Kind regards, Robert – RobB Aug 30 '20 at 07:02
  • Documentation: https://firebase.flutter.dev/, and the link in my answer. As far as I know `User() was not a built-in class before this upgrade, so it's probably a class of your own. My guess is that the new Firebase `User` class and your own existing class are conflicting. If that's the case, renaming your own class it the easiest fix. – Frank van Puffelen Aug 30 '20 at 14:34
  • Hi Frank, Sorry for my late comment! You where wright, User was a class I made my self. I resolved the conflict. it is working now! Thanks for your help! – RobB Oct 27 '20 at 09:35
0

You miss to add .currentUser() before .uid and jsut do this for all the other problems

But I hope to simplify you code just like that

final FirebaseAuth auth = FirebaseAuth.instance;

void inputData() async {
    final FirebaseUser user = await auth.currentUser();
    final uid = user.uid;
    // here you write the codes to input the data into firestore
  }
ElsayedDev
  • 601
  • 1
  • 8
  • 15
  • Thank you for your response! FirebaseUser seems to be depreciated. Do you have other suggestions? Kind regards, Robert – RobB Aug 29 '20 at 13:07