ios - 使用物理 iOS 设备在 Flutter 应用中使用 FirebaseAuth 登录时出错
问题描述
我已经使用 Flutter 和 FirebaseAuth 实现了一个简单的应用程序,我希望用户登录并提供电子邮件和密码,该应用程序在 iOS 模拟器中按预期工作但是,当我尝试将应用程序侧加载到物理 iOS 设备上时,我出现几个错误,登录过程失败,应用程序无法继续。我已经展示了代码、出现的错误,并列出了迄今为止我为减轻这种情况而采取的步骤,但这些步骤都没有奏效。
代码
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'screens/other/LoadingScreen.dart';
import 'screens/other/ErrorScreen.dart';
import 'screens/other/SignupScreen.dart';
import 'screens/other/HomeScreen.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
// This widget is the root of your application.
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<CovidHound> {
bool _initialized = false;
bool _error = false;
String _email = "";
String _password = "";
void initializeFlutterFire() async {
try {
await Firebase.initializeApp();
print("Init firebase");
setState(() {
_initialized = true;
});
} catch (e) {
print("Error init firebase:${e}");
setState(() {
_error = true;
});
}
}
Future<void> onTapSignIn() async {
try {
await FirebaseAuth.instance
.signInWithEmailAndPassword(email: _email, password: _password);
} on FirebaseAuthException catch (e) {
if (e.code == 'user-not-found') {
print('No user found for that email.');
} else if (e.code == 'wrong-password') {
print('Wrong password provided for that user.');
}
} catch (e) {
print("Error signing in: $e");
}
if (FirebaseAuth.instance.currentUser != null) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => HomeScreen(),
fullscreenDialog: true,
),
);
}
}
@override
void initState() {
super.initState();
initializeFlutterFire();
}
@override
Widget build(BuildContext context) {
if(_error) {
return ErrorScreen();
}
if (!_initialized) {
return LoadingScreen();
}
return MaterialApp(
home: Scaffold(
body: Center(
child: Column(
children: [
TextField(
decoration: InputDecoration(hintText: "Email"),
onChanged: (value) {
_email = value;
},
),
TextField(
decoration: InputDecoration(hintText: "Password"),
onChanged: (value) {
_password = value;
},
),
TextButton(
onPressed: () {
onTapSignIn();
},
child: Text("Sign In"),
),
],
),
),
),
);
}
}
错误
到目前为止,我已经尝试了以下方法,
- 根据文档正确配置 Firebase。
- 使用 flutter clean 清理 Xcode 工作区和构建。
- 将 iOS 和 Xcode 更新到最新版本。
- 升级颤振。
- 在 info.plist 中添加隐私权限 - 本地网络使用说明,如 ( https://flutter.dev/docs/development/add-to-app/ios/project-setup#local-network-privacy-permissions )中所示
解决方案
目前,您不等待您的initializeFlutterFire()
函数,这可能会导致您的错误消息,因为后续代码是在初始化 Firebase 之前执行的。
将您的initializeFlutterFire()
外部MyApp
或它的State
类移动,然后尝试将返回类型更改为,然后在(而不是 in )中Future<void>
调用此函数,例如:main()
initState()
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await initializeFlutterFire();
runApp(MyApp());
}
Firebase (FlutterFire) 要求您在启动 App 实例之前初始化插件以避免此类错误。
推荐阅读
- python - 在python中使用curve_fit拟合曲线时出错
- sql - 去除所有非字母数字字符和非集合标点
- flutter - 如何单击颤动中的按钮以更改背景图像
- flutter - 如何在 Flutter 中扩展 Rive 动画?
- java - 当我将项目从堆栈 1 移动到 2 时,这会为我返回堆栈空异常。你能帮我找出问题所在吗?
- sql - SQL 根据两列删除重复记录
- homebrew - 如何重新安装包含所有依赖项的包?
- javascript - 制作了连续的wix灯箱模态
- javascript - 图像不能是数组或对象
- twilio - 如何动态获取呼叫者电话号码并将其作为 Twilio 运行函数 wdget 中的 JSON 键之一传递?