PlatformException(error, Given String is empty or null, null) 错误颤动

Posted

技术标签:

【中文标题】PlatformException(error, Given String is empty or null, null) 错误颤动【英文标题】:PlatformException(error, Given String is empty or null, null) error flutter 【发布时间】:2020-09-24 06:06:50 【问题描述】:

嘿,伙计们,我正在尝试在我的应用程序中使用登录系统,但进度条始终显示登录但从未登录我可以注册,而且我可以在控制台的 firestore bur 端看到用户详细信息我收到了那个错误 "PlatformException(error, Given String is empty or null, null) error" 如果您有任何建议,是否可以解决该错误,请告诉我谢谢

          import 'package:cloud_firestore/cloud_firestore.dart';
          import 'package:firebase_auth/firebase_auth.dart';
          import 'package:flutter/cupertino.dart';
          import 'package:flutter/material.dart';
          import 'package:flutter/rendering.dart';
          import 'package:flutter/services.dart';
          import 'package:resat/src/pages/LoginPages/model/User.dart';
          import 'package:resat/src/pages/LoginPages/ui/home/HomeScreen.dart';
          import 'package:resat/src/pages/LoginPages/ui/services/Authenticate.dart';
          import 'package:resat/src/pages/LoginPages/ui/utils/helper.dart';
          import 'package:http/http.dart' as http;

          import '../../constants.dart' as Constants;
          import '../../main.dart';

          final _fireStoreUtils = FireStoreUtils();

          class LoginScreen extends StatefulWidget 
            @override
            State createState() 
              return _LoginScreen();
            
          

          class _LoginScreen extends State<LoginScreen> 
            TextEditingController _emailController = new TextEditingController();
            TextEditingController _passwordController = new TextEditingController();
            GlobalKey<FormState> _key = new GlobalKey();
            bool _validate = false;
            String email, password;

            @override
            Widget build(BuildContext context) 
              return Scaffold(
                appBar: AppBar(
                  backgroundColor: Colors.transparent,
                  iconTheme: IconThemeData(color: Colors.black),
                  elevation: 0.0,
                ),
                body: Form(
                  key: _key,
                  autovalidate: _validate,
                  child: ListView(
                    children: <Widget>[
                      Padding(
                        padding:
                        const EdgeInsets.only(top: 32.0, right: 16.0, left: 16.0),
                        child: Text(
                          'Sign In',
                          style: TextStyle(
                              color: Color(Constants.COLOR_PRIMARY),
                              fontSize: 25.0,
                              fontWeight: FontWeight.bold),
                        ),
                      ),
                      ConstrainedBox(
                        constraints: BoxConstraints(minWidth: double.infinity),
                        child: Padding(
                          padding:
                          const EdgeInsets.only(top: 32.0, right: 24.0, left: 24.0),
                          child: TextFormField(
                              textAlignVertical: TextAlignVertical.center,
                              textInputAction: TextInputAction.next,
                              validator: validateEmail,
                              onSaved: (String val) 
                                email = val;
                              ,
                              onFieldSubmitted: (_) =>
                                  FocusScope.of(context).nextFocus(),
                              controller: _emailController,
                              style: TextStyle(fontSize: 18.0),
                              keyboardType: TextInputType.emailAddress,
                              cursorColor: Color(Constants.COLOR_PRIMARY),
                              decoration: InputDecoration(
                                  contentPadding:
                                  new EdgeInsets.only(left: 16, right: 16),
                                  fillColor: Colors.white,
                                  hintText: 'E-mail Address',
                                  focusedBorder: OutlineInputBorder(
                                      borderRadius: BorderRadius.circular(25.0),
                                      borderSide: BorderSide(
                                          color: Color(Constants.COLOR_PRIMARY),
                                          width: 2.0)),
                                  border: OutlineInputBorder(
                                    borderRadius: BorderRadius.circular(25.0),
                                  ))),
                        ),
                      ),
                      ConstrainedBox(
                        constraints: BoxConstraints(minWidth: double.infinity),
                        child: Padding(
                          padding:
                          const EdgeInsets.only(top: 32.0, right: 24.0, left: 24.0),
                          child: TextFormField(
                              textAlignVertical: TextAlignVertical.center,
                              validator: validatePassword,
                              onSaved: (String val) 
                                email = val;
                              ,
                              onFieldSubmitted: (password) async 
                                await onClick(_emailController.text, password);
                              ,
                              textInputAction: TextInputAction.done,
                              style: TextStyle(fontSize: 18.0),
                              cursorColor: Color(Constants.COLOR_PRIMARY),
                              decoration: InputDecoration(
                                  contentPadding:
                                  new EdgeInsets.only(left: 16, right: 16),
                                  fillColor: Colors.white,
                                  hintText: 'Password',
                                  focusedBorder: OutlineInputBorder(
                                      borderRadius: BorderRadius.circular(25.0),
                                      borderSide: BorderSide(
                                          color: Color(Constants.COLOR_PRIMARY),
                                          width: 2.0)),
                                  border: OutlineInputBorder(
                                    borderRadius: BorderRadius.circular(25.0),
                                  ))),
                        ),
                      ),
                      Padding(
                        padding:
                        const EdgeInsets.only(right: 40.0, left: 40.0, top: 40),
                        child: ConstrainedBox(
                          constraints: const BoxConstraints(minWidth: double.infinity),
                          child: RaisedButton(
                            color: Color(Constants.COLOR_PRIMARY),
                            child: Text(
                              'Log In',
                              style:
                              TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
                            ),
                            textColor: Colors.white,
                            splashColor: Color(Constants.COLOR_PRIMARY),
                            onPressed: () async 
                              await onClick(
                                  _emailController.text, _passwordController.text);
                            ,
                            padding: EdgeInsets.only(top: 12, bottom: 12),
                            shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(25.0),
                                side:
                                BorderSide(color: Color(Constants.COLOR_PRIMARY))),
                          ),
                        ),
                      ),
                      Padding(
                        padding: const EdgeInsets.all(32.0),
                        child: Center(
                          child: Text(
                            'OR',
                            style: TextStyle(color: Colors.black),
                          ),
                        ),
                      ),

                    ],
                  ),
                ),
              );
            

            onClick(String email, String password) async 
              if (_key.currentState.validate()) 
                _key.currentState.save();
                showProgress(context, 'Logging in, please wait...', false);
                User user =
                await loginWithUserNameAndPassword(email, password.trim());
                if (user != null)
                  pushAndRemoveUntil(context, HomeScreen(user: user), false);

               else 
                setState(() 


                  _validate = true;
                );
              
            

            Future<User> loginWithUserNameAndPassword(String email,
                String password) async 
              try 
                AuthResult result = await FirebaseAuth.instance
                    .signInWithEmailAndPassword(email: email, password: password);
                DocumentSnapshot documentSnapshot = await FireStoreUtils.firestore
                    .collection(Constants.USERS)
                    .document(result.user.uid)
                    .get();
                User user;
                if (documentSnapshot != null && documentSnapshot.exists) 
                  user = User.fromJson(documentSnapshot.data);
                  user.active = true;
                  await _fireStoreUtils.updateCurrentUser(user, context);
                  hideProgress();
                  MyAppState.currentUser = user;
                
                return user;
               catch (exception) 
                hideProgress();
                switch ((exception as PlatformException).code) 
                  case 'ERROR_INVALID_EMAIL':
                    showAlertDialog(
                        context, 'Couldn\'t Authinticate', 'email address is malformed');
                    break;
                  case 'ERROR_WRONG_PASSWORD':
                    showAlertDialog(context, 'Couldn\'t Authinticate', 'wrong password');
                    break;
                  case 'ERROR_USER_NOT_FOUND':
                    showAlertDialog(context, 'Couldn\'t Authinticate',
                        'no user corresponding to the given email address');
                    break;
                  case 'ERROR_USER_DISABLED':
                    showAlertDialog(
                        context, 'Couldn\'t Authinticate', 'user has been disabled');
                    break;
                  case 'ERROR_TOO_MANY_REQUESTS':
                    showAlertDialog(context, 'Couldn\'t Authinticate',
                        'too many attempts to sign in as this user');
                    break;
                  case 'ERROR_OPERATION_NOT_ALLOWED':
                    showAlertDialog(context, 'Couldn\'t Authinticate',
                        'Email & Password accounts are not enabled');
                    break;
                
                print(exception.toString());
                return null;
              
            

            @override
            void dispose() 
              _emailController.dispose();
              _passwordController.dispose();
              super.dispose();
            
          

【问题讨论】:

你在哪一行得到错误.. 我在登录时遇到错误,所有行都在工作,只是没有登录 【参考方案1】:

您需要将_passwordController 绑定到该字段:

child: TextFormField(
      textAlignVertical: TextAlignVertical.center,
      validator: validatePassword,
      onSaved: (String val) 
        email = val;
      ,
      onFieldSubmitted: (password) async 
        await onClick(_emailController.text, _passwordController.text);
      ,
      controller : _passwordController,
      textInputAction: TextInputAction.done,
      style: TextStyle(fontSize: 18.0),

【讨论】:

以上是关于PlatformException(error, Given String is empty or null, null) 错误颤动的主要内容,如果未能解决你的问题,请参考以下文章

未处理的异常:PlatformException(错误,java.lang.IllegalStateException:尝试创建已创建的平台视图,视图 ID:0

PlatformException(错误,java.lang.IllegalStateException:尝试创建未注册类型的平台视图:plugins.flutter.io/webview

flutter_local_notifications:“PlatformException(PlatformException(INVALID_ICON,找不到资源))

PlatformException (PlatformException(sign_in_failed, com.google.android.gms.common.api.ApiException:

如何处理 Flutter 谷歌地图控制器

PlatformException(firebase_storage,位置不应是完整的 URL。,,null)