firebase - Flutter:将数据设置到 Firestore 不会写入任何数据,也不会抛出任何错误
问题描述
当我尝试在我的颤振应用程序中使用使用谷歌登录的用户写入 Firestore 时遇到问题。当用户按下使用 google 按钮登录时。它成功登录谷歌我可以在firebase控制台的身份验证页面中看到它。然后我从firestore读取用户是否在firestore中有数据,如果用户在firestore中没有文档,则返回null并创建它。它在调试模式下工作正常,但当我转到发布模式时它不起作用。当我将数据设置到 firestore 时,我没有抛出任何错误,所以就像 set 函数会成功写入 firestore。在发布模式下,如果我使用电子邮件和密码注册用户,它会在 Firestore 中创建用户配置文件而不会出现问题。我有两个项目,一个用于生产,一个用于开发。带有调试应用程序的开发项目工作正常。我已经清理了项目,我运行了颤振医生,一切似乎都很好。我还检查了 Firestore 规则,它们在两个项目中都是相同的。
这是在调试中工作的代码
Future<String> signInWithGoogle() async {
final GoogleSignInAccount googleSignInAccount = await googleSignIn.signIn();
final GoogleSignInAuthentication googleSignInAuthentication =
await googleSignInAccount.authentication;
final auth.AuthCredential credential = auth.GoogleAuthProvider.credential(
accessToken: googleSignInAuthentication.accessToken,
idToken: googleSignInAuthentication.idToken,
);
final auth.UserCredential authResult =
await _auth.signInWithCredential(credential);
final auth.User user = authResult.user;
assert(!user.isAnonymous);
assert(await user.getIdToken() != null);
final auth.User currentUser = await _auth.currentUser;
assert(user.uid == currentUser.uid);
var token = await FCMService().getDeviceToken();
DatabaseService databaseService = DatabaseService(uid: currentUser.uid);
var res = await databaseService.getUserData();
if(res == null) {
print("auth -> getUserDataFromFirestore: El usuario no existe, lo creamos en firestore con estos datos: "
"email: ${newUser.email}, nombre: ${newUser
.nombre}, token: ${newUser.notificationToken}, "
"telefono: ${newUser
.telefono}, tipo: ${newUser.tipo}, uid: ${newUser
.uid}");
User newUser = User(
email: currentUser.email,
nombre: currentUser.displayName,
notificationToken: token,
telefono: currentUser.phoneNumber,
tipo: TIPO_PERSONA_CLIENTE);
assert(await databaseService.setUserData(newUser) ==
"Ok");
}
return '${user.uid}';
}
Future setUserData(User user) async {
return await personasCollection.doc(uid).set({
'direccion': user.direccion,
'dob': user.dob,
'email': user.email,
'nombre': user.nombre,
'idEmpresa': user.idEmpresa,
'identificacion': user.identificacion,
'notificationToken': user.notificationToken,
'rolEmpresa': user.rolEmpresa,
'telefono': user.telefono,
'tipo': user.tipo,
}).then((val) {
return 'Ok';
}).catchError((val) {
return val.toString();
});
}
Future getUserData() async {
return await personasCollection.doc(uid).get()
.then((value){
return value.data();
})
.catchError((e){
throw "$e";
});
}
这是来自 UI 的 try catch
try {
var user = await _auth.signInWithGoogle();
print("sign_in -> usuario se inicio sesión con google, este es su id $user");
} catch (e) {
print("sign_in -> $e");
setState(() {
loading = false;
});
showSingleButtonDialog(
"Error",
"Algo salió mal, por favor inténtalo de nuevo",
"OK",
context);
}
这是调试:
✓ 内置 build/app/outputs/flutter-apk/app-release.apk (8.7MB)。I/flutter (3955): wrapper ->built E/FlutterFcmService(3955): Fatal: failed to find callback I/flutter (3955): wrapper -> built I/flutter (3955): wrapper -> el usuario ha iniciado sesion I/flutter (3955): database -> getUserData: get data del usuario: 6W6yF7OrSsaxDaQgHbBFwzbZjN72 I/flutter (3955): auth -> getUserDataFromFirestore: El usuario no existe, lo creamos en firestore con estos datos: email: dropit.informacion@gmail .com, nombre: Dropit Lo llevamos, token: cMteTk7lQsa4lkicSzxobj:APA91bHkjrfj6eJBivqCGqcDkih6FHlhPbwb1C_AKrveqHkeunudF6_QQvyR41XH7-z4lLI7sZSOFFwFOW_0QTWR1-zuSluYMO8ffT-ac8aR-4u_s6miU35XLffXIgzMSo3GAPur44Ge, telefono: null, tipo: 3, uid: 6W6yF7OrSsaxDaQgHbBFwzbZjN72 I/flutter ( 3955): sign_in -> usuario se inicio sesión con google ,
解决方案
错误出现在 signInWithGoogle() 函数中,在发布模式下颤动禁用断言。而不是断言使用 if
推荐阅读
- javascript - 脚本之间的 Google Chrome 消息 API 不起作用
- bash - 计算 bash 中的目录和文件
- swift - 如何使用协议将数据从视图控制器传递到其中的容器?
- matlab - While 和 If 循环 - 满足条件时返回两个结果
- php - DeviantArt 中的用户身份验证
- python - matplotlib.pyplot:绘图后共享轴的 x 轴不会将它们放在相同的 x 轴范围内
- python - 如何使用 Keras.to_Categorical 在 dataFrame 中一次对多个列进行 One-Hot 编码?
- html - IE 抓取 VBA 代码,例如航班信息搜索
- git - 对分叉仓库的新拉取请求显示已通过旧拉取请求合并的旧传出提交
- javascript - 服务器不呈现 ejs 文件