I'm trying to create a user system with assistment of this playlist. But, it's kind of outdated so I'm trying to keeping up with today. I'm a totally beginner. I read few things from the stackoverflow but I couldn't keep up. Here is what error it gave:
The argument type 'Stream<MyUser?>' can't be assigned to the parameter type 'Stream<MyUser?>?'.
And here is the main.dart: (To be specific error is right in the "value: AuthService().user," line. The "AuthService().user" part is underline in red.)
import 'package:flutter/material.dart';
import 'package:unistuff_main/screens/wrapper.dart';
import 'package:provider/provider.dart';
import 'package:unistuff_main/services/auth.dart';
import 'package:unistuff_main/models/myuser.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return StreamProvider<MyUser?>.value(
initialData: null,
value: AuthService().user,
child: MaterialApp(
home: Wrapper(),
),
);
}
}
And here is my myuser.dart file:
class MyUser {
final String? uid;
MyUser({this.uid});
}
And here is auth.dart: (If you noticed I didn't add any login method yet. Is that can cause the problem?)
import 'package:firebase_auth/firebase_auth.dart';
import 'package:unistuff_main/models/MyUser.dart';
class AuthService {
final FirebaseAuth _auth = FirebaseAuth.instance; //private data member
//create MyUser object based on FirebaseUser
MyUser? _userfromFirebase(User user) {
return user != null ? MyUser(uid: user.uid) : null;
}
//auth change user stream
Stream<MyUser?> get user {
return _auth
.authStateChanges()
.map((User? user) => _userfromFirebase(user!));
}
//sign in with email & password
Future signInWithMail() async {
try {} catch (e) {}
}
//register with email & password
//sign out
}
Thanks for any help..
CodePudding user response:
It seems that the type of your AuthService().user isn't null-able, so try to change the type of the user from your auth.dart to Stream<MyUser?>?.
Stream<MyUser?>? get onAuthStateChanges => FirebaseAuth.instance
.authStateChanges()
.map((currentUser) => MyUser.fromSnapshot(currentUser!));
I'm not sure, but you probably are using the authStateChanges() from FirebaseAuth.instance. So if it doesn't solve your problem, print the AuthService().user :)
CodePudding user response:
In the line when error occurs, try to use operator ?? and pass the raw object if null.
For example:
MyObject(
stringField: myString ?? '', //you are ensuring that if your String is null, there will be no error
);