首页 > 解决方案 > 使用物理 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"),
          ),
        ],
      ),
    ),
  ),
);

 }
}

错误

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

到目前为止,我已经尝试了以下方法,

  1. 根据文档正确配置 Firebase。
  2. 使用 flutter clean 清理 Xcode 工作区和构建。
  3. 将 iOS 和 Xcode 更新到最新版本。
  4. 升级颤振。
  5. 在 info.plist 中添加隐私权限 - 本地网络使用说明,如 ( https://flutter.dev/docs/development/add-to-app/ios/project-setup#local-network-privacy-permissions )中所示

标签: iosswiftfirebaseflutterdart

解决方案


目前,您不等待您的initializeFlutterFire()函数,这可能会导致您的错误消息,因为后续代码是在初始化 Firebase 之前执行的。

将您的initializeFlutterFire()外部MyApp或它的State类移动,然后尝试将返回类型更改为,然后在(而不是 in )中Future<void>调用此函数,例如:main()initState()

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await initializeFlutterFire();
  runApp(MyApp());
}

Firebase (FlutterFire) 要求您在启动 App 实例之前初始化插件以避免此类错误。


推荐阅读