gpt4 book ai didi

flutter - dart - 如何解决 'The argument type ' Null' can't be assigned to the parameter type 'MyUser' .'

转载 作者:行者123 更新时间:2023-12-04 07:27:52 33 4
gpt4 key购买 nike

我正在尝试实现一个简单的注册页面。以下是我的 main.dart 代码:

ma​​in.dart

void main() async {
await Firebase.initializeApp();
runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return StreamProvider<MyUser>.value(
value: AuthService().user,
initialData: null,
child: MaterialApp(
home: Wrapper(),
debugShowCheckedModeBanner: false,
)
);
}
}

我在 initialData: null, 行看到这个错误:

不能将参数类型“Null”分配给参数类型“MyUser”。

这是我的 MyUser 类:

user.dart

class MyUser {
final String uid;
MyUser({this.uid});
}

在第 3 行的 uid 上显示以下错误:

The parameter 'uid' can't have a value of 'null' because of its type, but the implicit default value is 'null'.
Try adding either an explicit non-'null' default value or the 'required' modifier.

我是 flutter 开发的新手,所以我不确定这意味着什么以及如何解决它。我无法在线找到任何相关帮助。任何帮助将不胜感激。

编辑 1:

整个 auth.dart 文件:

class AuthService {

final FirebaseAuth _auth = FirebaseAuth.instance;

// create user obj based on firebase user
MyUser? _userFromFirebaseUser(User user) {
// ignore: unnecessary_null_comparison
return user != null ? MyUser(uid: user.uid) : null;
}

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


// sign in with email and password
Future signInWithEmailAndPassword(String email, String password) async {
try {
UserCredential result = await _auth.signInWithEmailAndPassword(email: email, password: password);
User? user = result.user;
return _userFromFirebaseUser(user!);
} catch (error) {
print(error.toString());
return null;
}
}

// register with email and password
Future registerWithEmailAndPassword(String email, String password) async {
try {
UserCredential result = await _auth.createUserWithEmailAndPassword(email: email, password: password);
User? user = result.user;
return _userFromFirebaseUser(user!);
} catch (error) {
print(error.toString()+"oollala");
return null;
}
}

// sign out
Future signOut() async {
try {
return await _auth.signOut();
} catch (error) {
print(error.toString());
return null;
}
}
}

错误:

Stream<MyUser> get user {
return _auth.authStateChanges()
.map(_userFromFirebaseUser);
}

它向我显示 _userFromFirebaseUser 中的错误

参数类型 'MyUser? Function(User)' 不能分配给参数类型 'MyUser Function(User?)'。

最佳答案

您将属性 uid 定义为不可为 null 的 String,但由于 {},它在构造函数中被声明为可选值> 这意味着如果未分配,它将具有默认值 null

要修复此错误,您需要在构造函数中将 uid 设为非可选:

MyUser(this.uid);

或者让它成为必需的参数:

MyUser({required this.uid});

如果你的 uid 可以是 null 那么你需要像这样声明你的变量:

final String? uid; // this is a nullable String variable

关于flutter - dart - 如何解决 'The argument type ' Null' can't be assigned to the parameter type 'MyUser' .',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68127499/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com