无法通过登录屏幕颤动网络

Posted

技术标签:

【中文标题】无法通过登录屏幕颤动网络【英文标题】:Can't get pass login screen flutter web 【发布时间】:2021-01-13 03:05:59 【问题描述】:

我一直在使用 Firebase auth 和 Firestore,一切都可以在 android emu 上运行,但是当我在 Chrome 中吃午饭时,我无法通过登录, 我已经导入了 Firebase 为 index.html 提供的所有代码:

 <body>
   
<!-- The core Firebase JS SDK is always required and must be listed first -->
<script src="/__/firebase/7.21.1/firebase-app.js"></script>

<!-- TODO: Add SDKs for Firebase products that you want to use
     https://firebase.google.com/docs/web/setup#available-libraries -->
     <script src="/__/firebase/7.21.1/firebase-auth.js"></script>
     <script src="/__/firebase/7.21.1/firebase-firestore.js"></script>
   
<!-- Initialize Firebase -->
<script src="/__/firebase/init.js"></script>
<script>
  // Your web app's Firebase configuration
  var firebaseConfig = 
    apiKey: "AIzaSyCtfc......IstukN0Ci_mc",
    authDomain: "prettya1..91e.firebaseapp.com",
    databaseURL: "https://prettya.com",
    projectId: "prettyan..",
    storageBucket: "prettyanki-....com",
    messagingSenderId: "17870...",
    appId: "....."
  ;
  // Initialize Firebase
  firebase.initializeApp(firebaseConfig);

</script>
  <script>
    if ('serviceWorker' in navigator) 
      window.addEventListener('load', function () 
        navigator.serviceWorker.register('flutter_service_worker.js');
      );
    
  </script>
  <script src="main.dart.js" type="application/javascript"></script>
  
</body>

登录:

import 'package:PrettyAnki/services/auth.dart';
import 'package:PrettyAnki/shared/constants.dart';
import 'package:PrettyAnki/shared/loading.dart';
import 'package:flutter/material.dart';

class Register extends StatefulWidget 
  final Function toggleView;

  Register(this.toggleView);
  @override
  _RegisterState createState() => _RegisterState();


class _RegisterState extends State<Register> 
  List<Color> gradientcolors = [Color(0xFFcb2d3e), Color(0xFFef473a)];
  final AuthService _auth = AuthService();
  final _formkey = GlobalKey<FormState>();

  bool loading = false;

  //text field sate
  String email = "";
  String password = "";
  String error = "";

  @override
  Widget build(BuildContext context) 
    return loading
        ? Loading()
        : Scaffold(
            appBar: AppBar(
              flexibleSpace: Container(
                decoration: BoxDecoration(
                    gradient: LinearGradient(
                        begin: Alignment.topLeft,
                        end: Alignment.bottomRight,
                        colors: gradientcolors)),
              ),
              centerTitle: true,
              elevation: 0.0,
              title: RichText(
                text: TextSpan(children: <TextSpan>[
                  TextSpan(
                    text: "Pretty",
                    style: TextStyle(
                        color: Colors.white,
                        fontFamily: "Courgette",
                        fontSize: 25),
                  ),
                  TextSpan(text: "Anki", style: TextStyle(color: Colors.white))
                ]),
              ),
            ),
            body: Container(
                padding: EdgeInsets.symmetric(vertical: 20, horizontal: 50),
                child: Form(
                    key: _formkey,
                    child: Column(
                      children: [
                        SizedBox(
                          height: 20,
                        ),
                        Text(
                          "Welcome,",
                          style:
                              TextStyle(fontSize: 40, color: Colors.redAccent),
                        ),
                        SizedBox(
                          height: 20,
                        ),
                        TextFormField(
                          decoration:
                              textInputDecoration.copyWith(hintText: "email"),
                          validator: (val) =>
                              val.isEmpty ? "Enter an email" : null,
                          style: TextStyle(color: Colors.white),
                          onChanged: (val) 
                            setState(() 
                              email = val;
                            );
                          ,
                        ),
                        SizedBox(
                          height: 20,
                        ),
                        TextFormField(
                          decoration: textInputDecoration.copyWith(
                              hintText: "password"),
                          validator: (val) => val.length < 6
                              ? "Enter a password 6+ characters long"
                              : null,
                          style: TextStyle(color: Colors.white),
                          obscureText: true,
                          onChanged: (val) 
                            setState(() 
                              password = val;
                            );
                          ,
                        ),
                        SizedBox(
                          height: 20,
                        ),
                        TextFormField(
                          decoration: textInputDecoration.copyWith(
                              hintText: "confirm password"),
                          validator: (val) 
                            if (val != password) 
                              return "Passwords don't match";
                             else 
                              return null;
                            
                          ,
                          style: TextStyle(color: Colors.white),
                          obscureText: true,
                          onChanged: (val) 
                            setState(() 
                              password = val;
                            );
                          ,
                        ),
                        SizedBox(
                          height: 20,
                        ),
                        RaisedButton(
                            color: Color(0xFFcb2d3e),
                            child: Text(
                              "Sign Up",
                              style: TextStyle(color: Colors.white),
                            ),
                            onPressed: () async 
                              if (_formkey.currentState.validate()) 
                                setState(() 
                                  loading = true;
                                );
                                dynamic result =
                                    await _auth.registerWithEmailAndPassword(
                                        email, password, "");
                                if (result == null) 
                                  setState(() 
                                    error = "please supply a valid email";
                                    loading = false;
                                  );
                                
                              
                            ),
                        SizedBox(
                          height: 20,
                        ),
                        Text(
                          error,
                          style: TextStyle(color: Colors.white),
                        ),
                        SizedBox(
                          height: 20,
                        ),
                        FlatButton(
                            onPressed: () 
                              widget.toggleView();
                            ,
                            child: Text(
                              "Already have an account ? Sign In",
                              style: TextStyle(color: Colors.red[400]),
                            ))
                      ],
                    ))),
          );
  

我在互联网上搜索过,但 Firebase Flutter web 的教程很少,他们似乎没有这个问题。

有时我会收到此错误:

Error: MissingPluginException(No implementation found for method
startListeningAuthState on channel plugins.flutter.io/firebase_auth)
    at Object.throw_ [as throw] (http://localhost:55251/dart_sdk.js:4465:11)
    at MethodChannel._invokeMethod
    (http://localhost:55251/packages/flutter/src/services/platform_channel.dart.lib.js: 
   409:21)
    at _invokeMethod.next (<anonymous>)
    at http://localhost:55251/dart_sdk.js:37106:33
    at _RootZone.runUnary (http://localhost:55251/dart_sdk.js:36960:58)
    at _FutureListener.thenAwait.handleValue
    (http://localhost:55251/dart_sdk.js:32047:29)
    at handleValueCallback (http://localhost:55251/dart_sdk.js:32594:49)
    at Function._propagateToListeners (http://localhost:55251/dart_sdk.js:32632:17)     
    at _Future.new.[_completeWithValue] (http://localhost:55251/dart_sdk.js:32475:23)   
    at async._AsyncCallbackEntry.new.callback
    (http://localhost:55251/dart_sdk.js:32497:35)
    at Object._microtaskLoop (http://localhost:55251/dart_sdk.js:37221:13)
    at _startMicrotaskLoop (http://localhost:55251/dart_sdk.js:37227:13)
    at http://localhost:55251/dart_sdk.js:32849:9

发布规范:

dependencies:
  flutter:
    sdk: flutter

  

  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for ios style icons.
  cupertino_icons: ^0.1.3
  firebase_auth: ^0.14.0+5
  cloud_firestore: ^0.12.9+4
  firebase: ^7.3.0
  
  firebase_core: ^0.4.0
  provider: ^3.1.0
  curved_navigation_bar: ^0.3.3
  flutter_spinkit: ^4.0.0
  json_annotation: ^3.0.1
  flip_card: ^0.4.4
  carousel_slider: ^2.2.1

【问题讨论】:

您好,出于安全原因,您应该删除您发布的 apiKey 值。你有什么错误吗?请提供有关该问题的更多详细信息。 您共享的代码中没有登录屏幕。请确保您的问题包含minimal, complete/standalone code that reproduces the problem,因为它大大增加了有人可以提供帮助的机会。 @FrankvanPuffelen 谢谢,我添加了代码 @alexandrum,感谢您的建议,我有时会遇到错误,我在帖子中添加了它 @PaulCbt 您能否也添加您使用的 SDK 和 firebase 的版本?我怀疑版本不兼容。 【参考方案1】:

所以解决方案是,您需要更新依赖项,我的 pubscpec 现在:

dependencies:
  flutter:
    sdk: flutter

  

  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^0.1.3
  firebase_auth: ^0.18.0+1
  firebase_auth_web: ^0.3.0+1
  cloud_firestore: ^0.14.0+2
  firebase: ^7.3.0
  
  firebase_core: ^0.5.0
  provider: ^3.1.0
  curved_navigation_bar: ^0.3.3
  flutter_spinkit: ^4.0.0
  json_annotation: ^3.0.1
  flip_card: ^0.4.4
  carousel_slider: ^2.2.1

【讨论】:

以上是关于无法通过登录屏幕颤动网络的主要内容,如果未能解决你的问题,请参考以下文章

如何在颤动的任何屏幕上显示弹出消息?

在用户/登录/登录屏幕中颤动令人困惑的行为

Ontap 在颤动中无法使用 Google 地图标记

颤动的 Image.network 无法在发布 apk 上运行

通过网络应用程序登录FB后的空白屏幕?

将登录屏幕放在其中一张介绍幻灯片中以进行颤动