Firebase邮箱注册登录功能遇类型错误:String无法赋值给int
解决Firebase邮箱密码注册登录中的类型不匹配错误
问题根源
你的代码存在核心类型不匹配问题:
- Firebase Auth返回的用户
uid是字符串类型,但UserModel中uid被定义为int类型 - Firestore文档ID要求传入String类型,却使用了int类型的
userModel.uid - 错误返回场景中,传入空字符串给int类型的
uid参数
修复方案
核心修改是将UserModel中的uid类型从int改为String,同时调整相关代码适配该类型:
1. 修改UserModel类
import 'package:cloud_firestore/cloud_firestore.dart'; class UserModel { late final String uid; // 从int改为String late final String email; late final String displayName; late final String photoUrl; late final bool isAdmin; late final bool isGoogleUser; UserModel( {required this.uid, required this.email, required this.displayName, required this.photoUrl, this.isAdmin = false, this.isGoogleUser = false}); factory UserModel.fromFirestore(DocumentSnapshot snapshot) { return UserModel( uid: snapshot['uid'] as String, // 明确类型为String email: snapshot['email'] as String, displayName: snapshot['displayName'] as String, photoUrl: snapshot['photoUrl'] as String, isAdmin: snapshot['isAdmin'] ?? false, isGoogleUser: snapshot['isGoogleUser'] ?? false, ); } }
2. 调整注册功能代码中的错误点
修改后,注册时传入Firebase的String类型uid不再报错,Firestore文档ID也能正常使用String类型的uid,错误返回场景的空字符串参数也符合类型要求:
//Registering a new user unto firebase.................................................... Future<AuthStatus> registerWithEmailAndPassword( String username, String email, String password) async { try { final UserCredential userCredential = await _auth .createUserWithEmailAndPassword(email: email, password: password); if (userCredential.user != null) { UserModel userModel = UserModel( uid: userCredential.user!.uid, // 现在uid是String类型,匹配参数 email: email, displayName: username, photoUrl: ''); // Add the UserModel data to a Firestore collection await _usersRef.doc(userModel.uid).set({ // uid为String,符合doc()参数要求 'uid': userModel.uid, 'email': userModel.email, 'username': userModel.displayName, 'photoUrl': userModel.photoUrl, }); // Return a successful AuthStatus return AuthStatus( isSuccessful: true, errorMessage: '', userModel: userModel, ); } else { // If userCredential.user is null, registration failed return AuthStatus( isSuccessful: false, errorMessage: 'User registration failed', userModel: UserModel(uid: '', email: '', displayName: '', photoUrl: ''),// 空字符串符合String类型 ); } } catch (e) { // Handle any errors during registration return AuthStatus( isSuccessful: false, errorMessage: e.toString(), userModel: UserModel(uid: '', email: '', displayName: '', photoUrl: ''),// 空字符串符合String类型 ); } }
3. 调整登录功能代码中的错误点
//SignIn a registered member or user to firebase......................................... Future<UserModel?> signInWithEmailAndPassword( String username, String password) async { try { final UserCredential userCredential = await _auth.signInWithEmailAndPassword( email: username, password: password, ); if (userCredential.user != null) { UserModel userModel = UserModel( uid: userCredential.user!.uid, // 现在uid是String类型,匹配参数 email: userCredential.user!.email ?? '', displayName: userCredential.user!.displayName ?? '', photoUrl: '', ); return userModel; } else { return null; } } catch (e) { print('Sign-in error: $e'); return null; } }
关键修改总结
- 统一
uid类型为String,匹配Firebase生态的类型规范 - 确保Firestore文档ID使用String类型的用户uid
- 错误场景中使用空字符串作为默认uid,符合类型要求
内容的提问来源于stack exchange,提问作者Fred_Wolfe
相关产品推荐
相关产品推荐

