无法在 Flutter 中使用 Firebase Auth 进行注册

Ket*_*eke 5 android firebase firebase-authentication flutter

我正在为我的应用程序使用 Firebase,我已正确设置所有内容,并且 Firebase Firestore 工作正常,没有任何问题,我能够在那里读取和写入数据,但是当我尝试在 Firebase 中创建用户时,我在调试控制台中收到此消息:

\n\n
I/BiChannelGoogleApi( 2228): [FirebaseAuth: ] getGoogleApiForMethod() returned Gms: com.google.firebase.auth.api.internal.zzaq@fc008c3\nW/DynamiteModule( 2228): Local module descriptor class for com.google.firebase.auth not found.\nI/FirebaseAuth( 2228): [FirebaseAuth:] Preparing to create service connection to gms implementation\nI/lutter_firebase( 2228): type=1400 audit(0.0:1201): avc: denied { sendto } for path="/dev/socket/logdw" scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:r:init:s0 tclass=unix_dgram_socket permissive=1\nE/GmsClientSupervisor( 2228): Timeout waiting for ServiceConnection callback com.google.firebase.auth.api.gms.service.START\nE/GmsClientSupervisor( 2228): java.lang.Exception\nE/GmsClientSupervisor( 2228):   at com.google.android.gms.common.internal.zze.handleMessage(Unknown Source:53)\nE/GmsClientSupervisor( 2228):   at android.os.Handler.dispatchMessage(Handler.java:101)\nE/GmsClientSupervisor( 2228):   at com.google.android.gms.internal.common.zze.dispatchMessage(Unknown Source:8)\nE/GmsClientSupervisor( 2228):   at android.os.Looper.loop(Looper.java:164)\nE/GmsClientSupervisor( 2228):   at android.app.ActivityThread.main(ActivityThread.java:6541)\nE/GmsClientSupervisor( 2228):   at java.lang.reflect.Method.invoke(Native Method)\nE/GmsClientSupervisor( 2228):   at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)\nE/GmsClientSupervisor( 2228):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)\nW/DynamiteModule( 2228): Local module descriptor class for com.google.firebase.auth not found.\nI/FirebaseAuth( 2228): [FirebaseAuth:] Preparing to create service connection to gms implementation\n
Run Code Online (Sandbox Code Playgroud)\n\n

这是pubspec.yaml文件:

\n\n
name: chat_app_flutter_firebase\ndescription: A new Flutter project.\n\nversion: 1.0.0+1\n\nenvironment:\n  sdk: ">=2.3.0 <3.0.0"\n\ndependencies:\n  flutter:\n    sdk: flutter\n  cupertino_icons: ^0.1.2\n  cloud_firestore: ^0.13.5\n  firebase_auth: ^0.16.0\n\n\ndev_dependencies:\n  flutter_test:\n    sdk: flutter\n\nflutter:\n\n  uses-material-design: true\n
Run Code Online (Sandbox Code Playgroud)\n\n

auth_screen.dart

\n\n
import \'package:flutter/material.dart\';\nimport \'package:firebase_auth/firebase_auth.dart\';\nimport \'package:flutter/services.dart\';\nimport \'package:cloud_firestore/cloud_firestore.dart\';\n\nimport \'../widgets/auth/auth_form.dart\';\n\nclass AuthScreen extends StatefulWidget {\n  @override\n  _AuthScreenState createState() => _AuthScreenState();\n}\n\nclass _AuthScreenState extends State<AuthScreen> {\n  final _auth = FirebaseAuth.instance;\n  var _isLoading = false;\n\n  void _submitAuthForm(\n    String email,\n    String password,\n    String username,\n    bool isLogin,\n    BuildContext ctx,\n  ) async {\n    AuthResult authResult;\n\n    try {\n      setState(() {\n        _isLoading = true;\n      });\n      if (isLogin) {\n        authResult = await _auth.signInWithEmailAndPassword(\n          email: email,\n          password: password,\n        );\n      } else {\n        authResult = await _auth.createUserWithEmailAndPassword(\n          email: email,\n          password: password,\n        );\n        await Firestore.instance\n            .collection(\'users\')\n            .document(authResult.user.uid)\n            .setData({\n          \'username\': username,\n          \'email\': email,\n        });\n      }\n    } on PlatformException catch (err) {\n      var message = \'An error occurred, pelase check your credentials!\';\n\n      if (err.message != null) {\n        message = err.message;\n      }\n\n      Scaffold.of(ctx).showSnackBar(\n        SnackBar(\n          content: Text(message),\n          backgroundColor: Theme.of(ctx).errorColor,\n        ),\n      );\n      setState(() {\n        _isLoading = false;\n      });\n    } catch (err) {\n      print(err);\n      setState(() {\n        _isLoading = false;\n      });\n    }\n  }\n\n  @override\n  Widget build(BuildContext context) {\n    return Scaffold(\n      backgroundColor: Theme.of(context).primaryColor,\n      body: AuthForm(\n        _submitAuthForm,\n        _isLoading,\n      ),\n    );\n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

auth_form.dart

\n\n
import \'package:flutter/material.dart\';\n\nclass AuthForm extends StatefulWidget {\n  AuthForm(\n    this.submitFn,\n    this.isLoading,\n  );\n\n  final bool isLoading;\n  final void Function(\n    String email,\n    String password,\n    String userName,\n    bool isLogin,\n    BuildContext ctx,\n  ) submitFn;\n\n  @override\n  _AuthFormState createState() => _AuthFormState();\n}\n\nclass _AuthFormState extends State<AuthForm> {\n  final _formKey = GlobalKey<FormState>();\n  var _isLogin = true;\n  var _userEmail = \'\';\n  var _userName = \'\';\n  var _userPassword = \'\';\n\n  void _trySubmit() {\n    final isValid = _formKey.currentState.validate();\n    FocusScope.of(context).unfocus();\n\n    if (isValid) {\n      _formKey.currentState.save();\n      widget.submitFn(_userEmail.trim(), _userPassword.trim(), _userName.trim(),\n          _isLogin, context);\n    }\n  }\n\n  @override\n  Widget build(BuildContext context) {\n    return Center(\n      child: Card(\n        margin: EdgeInsets.all(20),\n        child: SingleChildScrollView(\n          child: Padding(\n            padding: EdgeInsets.all(16),\n            child: Form(\n              key: _formKey,\n              child: Column(\n                mainAxisSize: MainAxisSize.min,\n                children: <Widget>[\n                  TextFormField(\n                    key: ValueKey(\'email\'),\n                    validator: (value) {\n                      if (value.isEmpty || !value.contains(\'@\')) {\n                        return \'Please enter a valid email address.\';\n                      }\n                      return null;\n                    },\n                    keyboardType: TextInputType.emailAddress,\n                    decoration: InputDecoration(\n                      labelText: \'Email address\',\n                    ),\n                    onSaved: (value) {\n                      _userEmail = value;\n                    },\n                  ),\n                  if (!_isLogin)\n                    TextFormField(\n                      key: ValueKey(\'username\'),\n                      validator: (value) {\n                        if (value.isEmpty || value.length < 4) {\n                          return \'Please enter at least 4 characters\';\n                        }\n                        return null;\n                      },\n                      decoration: InputDecoration(labelText: \'Username\'),\n                      onSaved: (value) {\n                        _userName = value;\n                      },\n                    ),\n                  TextFormField(\n                    key: ValueKey(\'password\'),\n                    validator: (value) {\n                      if (value.isEmpty || value.length < 7) {\n                        return \'Password must be at least 7 characters long.\';\n                      }\n                      return null;\n                    },\n                    decoration: InputDecoration(labelText: \'Password\'),\n                    obscureText: true,\n                    onSaved: (value) {\n                      _userPassword = value;\n                    },\n                  ),\n                  SizedBox(height: 12),\n                  if (widget.isLoading) CircularProgressIndicator(),\n                  if (!widget.isLoading)\n                    RaisedButton(\n                      child: Text(_isLogin ? \'Login\' : \'Signup\'),\n                      onPressed: _trySubmit,\n                    ),\n                  if (!widget.isLoading)\n                    FlatButton(\n                      textColor: Theme.of(context).primaryColor,\n                      child: Text(_isLogin\n                          ? \'Create new account\'\n                          : \'I already have an account\'),\n                      onPressed: () {\n                        setState(() {\n                          _isLogin = !_isLogin;\n                        });\n                      },\n                    )\n                ],\n              ),\n            ),\n          ),\n        ),\n      ),\n    );\n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

我之前曾使用较旧的 Flutter SDK 开发过一个类似的应用程序,一切都运行良好。

\n\n

当前 Flutter SDK 信息:

\n\n

Flutter 1.17.0 \xe2\x80\xa2 通道稳定 \xe2\x80\xa2 https://github.com/flutter/flutter.git
\n框架 \xe2\x80\xa2 修订版 e6b34c2b5c(9 天前) \xe2\x80 \xa2 2020-05-02 11:39:18 -0700 ,\n引擎 \xe2\x80\xa2 修订版 540786dd51\n工具 \xe2\x80\xa2 Dart 2.8.1

\n\n

GitHub 存储库GitHub 存储库

\n\n

任何帮助将不胜感激

\n

Ket*_*eke 1

问题出在我的模拟器上,在物理设备上运行它并且它可以工作。

我使用的是 Genymotion 模拟器,但它没有安装 Play Store,所以我使用 GApps 安装它们,这也解决了模拟器的问题。

此外,使用具有 Play Store 服务的 Android Studio 模拟器也能达到目的。