尝试使用电子邮件和密码创建帐户时,Flutter 应用程序崩溃
Posted
技术标签:
【中文标题】尝试使用电子邮件和密码创建帐户时,Flutter 应用程序崩溃【英文标题】:Flutter app is crashing when trying to creating account with email and password 【发布时间】:2019-05-22 17:45:25 【问题描述】:这是我创建帐户的代码,但是当电子邮件 ID 已注册时,应用程序崩溃并且我的 try catch 方法无法捕获它。 如何解决这个问题?
void careteAccount() async
if (in_email.text.trim().length > 0 && in_password.text.trim().length > 0)
try
// FirebaseAuth.instance.verifyPhoneNumber(phoneNumber: "+919656936681", codeAutoRetrievalTimeout: (String verificationId) , codeSent: (String verificationId, int forceResendingToken) , timeout: null, verificationCompleted: (FirebaseUser firebaseUser) , verificationFailed: (AuthException error) );
FirebaseUser user = await FirebaseAuth.instance
.createUserWithEmailAndPassword(
email: in_email.value.text.trim(), password: in_password.value.text.trim())
.catchError((e)
print(e.toString());
Fluttertoast.showToast(
msg: e.toString(),
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.TOP,
timeInSecForios: 1,
backgroundColor: Colors.black54,
textColor: Colors.white);
)
.then((u)
if(u!=null?u.email!=null?true:false:false)
print("account created "+u.email);
else
print("account creatin failed");
);
print("create accound in $user.email");
catch (e)
print(e.toString());
Fluttertoast.showToast(
msg: e.toString(),
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.TOP,
timeInSecForIos: 1,
backgroundColor: Colors.black54,
textColor: Colors.white);
print('Error $e');
else
print("login called and countroler is null");
错误消息“给定的密码无效。[密码应至少为 6 个字符]”
我需要以编程方式解决错误,而不是像上图那样
D/ (8896): HostConnection::get() 新主机连接建立 0xb40f0c90, tid 8923 D/EGL_emulation(8896): eglMakeCurrent:0xb42bc4c0:ver 2 0 W/BiChannelGoogleApi(8896): [FirebaseAuth: ] getGoogleApiForMethod() 返回 Gms: com.google.firebase.auth.api.internal.zzal@3b1fc27b
成功创建帐户时出错
I/flutter ( 9012): account created milla@gmail.com
Unhandled exception:
NoSuchMethodError: The getter 'className' was called on null.
Receiver: null
Tried calling: className
#0 Object.noSuchMethod (dart:core/runtime/libobject_patch.dart:50:5)
#1 serializeProcedure (package:front_end/src/fasta/kernel/utils.dart:99:29)
#2 FrontendCompiler.compileExpression (package:vm/frontend_server.dart:482:16)
<asynchronous suspension>
#3 _FlutterFrontendCompiler.compileExpression (package:frontend_server/server.dart:62:22)
#4 listenAndCompile.<anonymous closure> (package:vm/frontend_server.dart:797:20)
<asynchronous suspension>
#5 _RootZone.runUnaryGuarded (dart:async/zone.dart:1314:10)
#6 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:336:11)
#7 _BufferingStreamSubscription._add (dart:async/stream_impl.dart:263:7)
#8 _SinkTransformerStreamSubscription._add (dart:async/stream_transformers.dart:68:11)
#9 _EventSinkWrapper.add (dart:async/stream_transformers.dart:15:11)
#10 _StringAdapterSink.add (dart:convert/string_conversion.dart:268:11)
#11 _LineSplitterSink._addLines (dart:convert/line_splitter.dart:154:13)
#12 _LineSplitterSink.addSlice (dart:convert/line_splitter.dart:129:5)
#13 StringConversionSinkMixin.add (dart:convert/string_conversion.dart:189:5)
#14 _SinkTransformerStreamSubscription._handleData (dart:async/stream_transformers.dart:120:24)
#15 _RootZone.runUnaryGuarded (dart:async/zone.dart:1314:10)
#16 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:336:11)
#17 _BufferingStreamSubscription._add (dart:async/stream_impl.dart:263:7)
#18 _SinkTransformerStreamSubscription._add (dart:async/stream_transformers.dart:68:11)
#19 _EventSinkWrapper.add (dart:async/stream_transformers.dart:15:11)
#20 _StringAdapterSink.add (dart:convert/string_conversion.dart:268:11)
#21 _StringAdapterSink.addSlice (dart:convert/string_conversion.dart:273:7)
#22 _Utf8ConversionSink.addSlice (dart:convert/string_conversion.dart:348:20)
#23 _Utf8ConversionSink.add (dart:convert/string_conversion.dart:341:5)
#24 _ConverterStreamEventSink.add (dart:convert/chunked_conversion.dart:86:18)
#25 _SinkTransformerStreamSubscription._handleData (dart:async/stream_transformers.dart:120:24)
#26 _RootZone.runUnaryGuarded (dart:async/zone.dart:1314:10)
#27 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:336:11)
#28 _BufferingStreamSubscription._add (dart:async/stream_impl.dart:263:7)
【问题讨论】:
【参考方案1】:在 firebase 中,密码必须至少为 6 个字符。
【讨论】:
我需要在程序上捕获错误,而不是像崩溃错误 我不明白解决方案 你能帮帮我吗?【参考方案2】:为了使代码更安全,避免抛出异常,您必须在 firebase 中创建身份验证帐户或登录时检查代码是否抛出它
例如在你的状态中使用这个在你的 FirebaseAuthUtil class
中添加这个方法作为例子
static Future<dynamic> createEmailAndPassword(String email,String pass) async
FirebaseAuth mAuth = FirebaseAuth.instance;
try
AuthResult result =await mAuth.createUserWithEmailAndPassword(email: email, password: pass);
FirebaseUser userFirebase = await mAuth.currentUser();
if (userFirebase != null && result.user.uid == userFirebase.uid)
return userFirebase;
else
return PlatformException(code: "-1", message: "error assign uid");
on PlatformException catch (ex)
debugPrint("ex :$ex.code");
return ex;
并从您的注册页面调用上述方法
dynamic result = FirebaseAuthUtil.createEmailAndPassword(email, pass);
if(result is FirebaseUser )
// create account is success
else if(result is PlatformException)
debugPrint("ereor signUp :$result.code");
【讨论】:
以上是关于尝试使用电子邮件和密码创建帐户时,Flutter 应用程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章
Flutter 在尝试 Firebase 身份验证时无法捕获 PlatformException
无法在 Xcode 的 iphone 模拟器中输入密码来创建帐户
如何合并两个 Firebase 帐户(一个使用电子邮件创建,另一个使用电话号码创建)?
当我注销时,我无法使用相同的电子邮件和密码登录,显示错误 jwt