簡體   English   中英

在使用物理 iOS 設備的 Flutter 應用程序中使用 FirebaseAuth 登錄時出錯

[英]Error signing in using FirebaseAuth in a Flutter app using a physical iOS device

I have implemented a simple application using Flutter and FirebaseAuth where I want a user to sign in giving an email and a password, this application works as intended in the iOS simulators however, when I try side loading the application on to a physical iOS device I出現幾個錯誤,登錄過程失敗,應用程序無法繼續。 我已經展示了代碼、出現的錯誤,並列出了迄今為止我為減輕這種情況而采取的步驟,但這些步驟都沒有奏效。

代碼

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 清潔 Xcode 工作區和構建。
  3. 將 iOS 和 Xcode 更新到最新版本。
  4. 升級 Flutter。
  5. 在 info.plist 中添加 Privacy - Local Network Usage Description 的權限,如 ( https://flutter.dev/docs/development/add-to-app/ios/project-setup#local-network-privacy-permissions中所示)

目前,您無需等待您的initializeFlutterFire() function,這可能會導致您的錯誤消息,因為后續代碼在 Firebase 初始化之前執行。

MyApp之外或它是State initializeFlutterFire() main()然后嘗試將返回類型更改為Future<void> initState()

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

Firebase (FlutterFire) 要求您在啟動應用程序實例之前初始化插件以避免此類錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM