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