简体   繁体   中英

The class 'User' doesn't have a default constructor -flutter

I am having a problem with my flutter app. I am following a flutter-firebase tutorial by 'The Net Ninja', but I added '?' after 'User' to make it null-able.

However, I am still getting this error in main.dart on 'AuthService().user' line 10: 'the argument type 'Stream<User?>? (where User is defined in C:...\firebase_auth-3.2.0\lib\src\user.dart' can't be assigned to the parameter type 'Stream<User?>? (where User is defined in C:...\my_app\lib\models\user.dart)'

And an error in auth.dart on 'User' line 16: the class 'User' doesn't have a default constructor.

I tried adding and removing the '?' everywhere before and after the '>' but that didn't help. also I cannot add '?' after 'User' in line 16 because it gives an error on 'uid'.

this is my main.dart:

import 'package:flutter/material.dart';
import 'package:irrigationapp/screens/authenticate/sign_in.dart';
import 'package:irrigationapp/screens/wrapper.dart';
import 'package:irrigationapp/services/auth.dart';
import 'package:irrigationapp/models/user.dart';
import 'screens/language.dart';
import 'package:provider/provider.dart';

void main() => runApp(StreamProvider<User?>.value(
  value: AuthService().user,
  initialData: null,
  child:   MaterialApp(
  
      debugShowCheckedModeBanner: false,
  
      home: Wrapper()
  
  
  
  ),
));

this is my auth.dart:

import 'package:firebase_auth/firebase_auth.dart';
import 'package:irrigationapp/models/user.dart' as UserModal;
import 'package:irrigationapp/services/database.dart';

class AuthService {

  final FirebaseAuth _auth = FirebaseAuth.instance;

  //create a user object based on FirebaseUser (the return type is user then)
  //underscore cz this is a private function that we can only use here.
  //if it's true return the uid, else return null.
  // ignore: unused_element
  User? _userFromFirebaseUser(User? user)  {
    // ignore: unnecessary_null_comparison
    if (user !=null) {
      return User?(uid: user.uid);
    }else{
      return null;
    }
  }

  //auth change user stream
  Stream<User?>? get user{
    return _auth.authStateChanges()
        .map(_userFromFirebaseUser);
  }

  //sign in anonymously
  Future signInAnon() async{
    try{
      UserCredential result = await _auth.signInAnonymously();
      User? user = result.user;
      return _userFromFirebaseUser(user!);
    }
    catch(e){
      // ignore: avoid_print
      print(e.toString());
      return null;
    }
  }

  //sign in using mail and pass
  Future signInWithEmailAndPassword(String email, String password) async{
    try{
      UserCredential result = await _auth.signInWithEmailAndPassword(email: email, password: password);
      // ignore: unused_local_variable
      User? user = result.user;
      return _userFromFirebaseUser(user!);
    }catch(e){
      // ignore: avoid_print
      print(e.toString());
      return null;
    }
  }

}

Thank you for your help

package:firebase_auth renamed its FirebaseUser class to just User . Presumably you tried to fix this by doing a search and replace through your code to change FirebaseUser to just User , but this created a name collision with your own User class. You then tried to avoid this collision by adding a library prefix for your class but didn't properly fix all use sites.

( The Firebase User class does not provide any public constructors, which caught the error.)

You have a function:

  //create a user object based on FirebaseUser (the return type is user then)
  //underscore cz this is a private function that we can only use here.
  //if it's true return the uid, else return null.
  // ignore: unused_element
  User? _userFromFirebaseUser(User? user)  {
    // ignore: unnecessary_null_comparison
    if (user !=null) {
      return User(uid: user.uid);
    }else{
      return null;
    }
  }

which currently doesn't make sense because it takes a User as an argument and then returns the same type. The function name and description indicate that you instead intend to accept a Firebase User object and to return your own User object, presumably from package:irrigationapp/models/user.dart which you've prefixed with UserModel . You therefore would need to change it to:

  //create a user object based on FirebaseUser (the return type is user then)
  //underscore cz this is a private function that we can only use here.
  //if it's true return the uid, else return null.
  // ignore: unused_element
  UserModel.User? _userFromFirebaseUser(User? user)  {
    // ignore: unnecessary_null_comparison
    if (user !=null) {
      return UserModel.User(uid: user.uid);
    }else{
      return null;
    }
  }

(Personally I think it'd be better to add a library prefix for the Firebase classes instead, though.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM