【问题标题】:Flutter error, Null check operator used on a null value颤振错误,对空值使用空检查运算符
【发布时间】:2021-09-25 08:40:48
【问题描述】:

每当我尝试进行注册并点击注册按钮时,我都会遇到用于空值的空检查运算符的此类问题。 我收到此错误消息

══════ 手势捕获的异常════════════════════

在处理手势时引发以下 _CastError: 用于空值的空检查运算符

当异常被抛出时,这是堆栈

#0 _AuthorizationState.build。 包:gr_space/screens/profile_screen.dart:244

#1 _InkResponseState._handleTap 包:flutter/…/material/ink_well.dart:989

#2 GestureRecognizer.invokeCallback 包:flutter/…/gestures/recognizer.dart:182

#3 TapGestureRecognizer.handleTapUp

package:flutter/…/gestures/tap.dart:607

#4 BaseTapGestureRecognizer._checkUp 包:flutter/…/gestures/tap.dart:296

... 处理程序:“onTap” 识别器:TapGestureRecognizer#f8d29 debugOwner:手势检测器 状态:就绪 赢得竞技场 finalPosition: 偏移量(215.0, 716.5) finalLocalPosition: 偏移量(199.0, 16.0) 按钮:1 发送点击向下 ══════════════════════════════════════════════════ ═══════

这是我的身份验证屏幕。飞镖文件

import 'package:flutter/material.dart';

enum AuthMode { Signup, Login }

class ProfileScreen extends StatelessWidget {
  static const routeName = '/profile';
  @override
  Widget build(BuildContext context) {
    return AnimatedContainer(
      duration: Duration(milliseconds: 300),
      child: Authorization(),
    );
  }
}

class Authorization extends StatefulWidget {
  @override
  _AuthorizationState createState() => _AuthorizationState();
}

class _AuthorizationState extends State<Authorization> {
  final GlobalKey<FormState> _formKey = GlobalKey();
  AuthMode _authMode = AuthMode.Login;
  final _passwordController = TextEditingController();
  Map<String, String> _authData = {
    'firstName': '',
    'lastName': '',
    'username': '',
    'email': '',
    'phoneNumber': '',
    'dateOfBirth': '',
    'password': '',
    'confirmPassword': '',
    'city': '',
    'gender': '',
  };
  void _switchAuthMode() {
    if (_authMode == AuthMode.Login) {
      setState(() {
        _authMode = AuthMode.Signup;
      });
    } else {
      setState(() {
        _authMode = AuthMode.Login;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    final deviceSize = MediaQuery.of(context).size;

    return Container(
      height: _authMode == AuthMode.Signup ? 320 : 260,
      constraints:
          BoxConstraints(minHeight: _authMode == AuthMode.Signup ? 620 : 660),
      width: deviceSize.width,
      padding: EdgeInsets.all(16.0),
      child: Form(
        child: SingleChildScrollView(
          child: Column(
            children: [
              TextFormField(
                decoration: InputDecoration(labelText: 'First name'),
                keyboardType: TextInputType.name,
                // ignore: missing_return
                validator: (value) {
                  if (value!.isEmpty) {
                    return 'Required';
                  }
                },
                onSaved: (value) {
                  _authData['firstName'] = value!;
                },
              ),
              TextFormField(
                decoration: InputDecoration(labelText: 'Last name'),
                keyboardType: TextInputType.emailAddress,
                // ignore: missing_return
                validator: (value) {
                  if (value!.isEmpty) {
                    return 'Required';
                  }
                },
                onSaved: (value) {
                  _authData['lastName'] = value!;
                },
              ),
              TextFormField(
                decoration: InputDecoration(labelText: 'Username'),
                keyboardType: TextInputType.name,
                // ignore: missing_return
                validator: (value) {
                  if (value!.isEmpty) {
                    return 'Required';
                  }
                },
                onSaved: (value) {
                  _authData['username'] = value!;
                },
              ),
              TextFormField(
                decoration: InputDecoration(labelText: 'E-Mail'),
                keyboardType: TextInputType.emailAddress,
                // ignore: missing_return
                validator: (value) {
                  if (value!.isEmpty || !value.contains('@')) {
                    return 'Invalid email!';
                  }
                },
                onSaved: (value) {
                  _authData['email'] = value!;
                },
              ),
              TextFormField(
                decoration: InputDecoration(labelText: 'Password'),
                obscureText: true,
                controller: _passwordController,
                // ignore: missing_return
                validator: (value) {
                  if (value!.isEmpty || value.length < 5) {
                    return 'Password is too short!';
                  }
                },
                onSaved: (value) {
                  _authData['password'] = value!;
                },
              ),
              (_authMode == AuthMode.Login)
                  ? Container(
                      child: Column(children: [
                        TextFormField(
                          enabled: _authMode == AuthMode.Signup,
                          decoration:
                              InputDecoration(labelText: 'Confirm Password'),
                          obscureText: true,
                          validator: _authMode == AuthMode.Signup
                              // ignore: missing_return
                              ? (value) {
                                  if (value != _passwordController.text) {
                                    return 'Passwords do not match!';
                                  }
                                }
                              : null,
                          onSaved: (value) {
                            _authData['confirmPassword'] = value!;
                          },
                        ),
                        TextFormField(
                          decoration:
                              InputDecoration(labelText: 'Phone Number'),
                          keyboardType: TextInputType.phone,
                          // ignore: missing_return
                          validator: (value) {
                            if (value!.isEmpty || value.length < 7) {
                              return 'Invalid phone number!';
                            }
                          },
                          onSaved: (value) {
                            _authData['phone number'] = value!;
                          },
                        ),
                        TextFormField(
                          decoration: InputDecoration(labelText: 'City'),
                          keyboardType: TextInputType.emailAddress,
                          // ignore: missing_return
                          validator: (value) {
                            if (value!.isEmpty) {
                              return 'Required';
                            }
                          },
                          onSaved: (value) {
                            _authData['city'] = value!;
                          },
                        ),
                        TextFormField(
                          decoration: InputDecoration(labelText: 'Gender'),
                          keyboardType: TextInputType.emailAddress,
                          // ignore: missing_return
                          validator: (value) {
                            if (value!.isEmpty) {
                              return 'Required';
                            }
                          },
                          onSaved: (value) {
                            _authData['gender'] = value!;
                          },
                        ),
                      ]),
                    )
                  : SizedBox(
                      height: 10,
                    ),
              SizedBox(
                height: 20,
              ),
              MaterialButton(
                height: 40,
                color: Theme.of(context).primaryColor,
                minWidth: double.infinity,
                onPressed: () {
                  if(_formKey.currentState!.validate()){
                    print('yess');
                  }
                },
                elevation: 0,
                //shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
                child: Text(
                  'Register',
                  style: TextStyle(
                    color: Colors.white,
                  ),
                ),
              ),
              Divider(
                color: Colors.black,
              ),
              MaterialButton(
                height: 40,
                color: Color.fromARGB(255, 222, 82, 69),
                minWidth: double.infinity,
                onPressed: () {},
                elevation: 0,
                //shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Image.asset(
                      'assets/gmail_logo.png',
                      height: 25,
                      width: 25,
                    ),
                    SizedBox(
                      width: 15,
                    ),
                    Text(
                      'Register with gmail',
                      style: TextStyle(
                        color: Colors.white,
                      ),
                    ),
                  ],
                ),
              ),
              MaterialButton(
                height: 40,
                color: Color.fromARGB(255, 65, 103, 178),
                minWidth: double.infinity,
                onPressed: () {},
                elevation: 0,
                //shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Image.asset(
                      'assets/facebook_logo.png',
                      height: 25,
                      width: 25,
                    ),
                    SizedBox(
                      width: 15,
                    ),
                    Text(
                      'Register with Facebook',
                      style: TextStyle(
                        color: Colors.white,
                      ),
                    ),
                  ],
                ),
              ),
              SizedBox(
                height: 20,
              ),
              Text(
                'Already have an account?',
              ),
              SizedBox(
                height: 10,
              ),
              TextButton(
                onPressed: _switchAuthMode,
                child: Text(
                  'Sign In',
                  style: TextStyle(
                    color: Colors.blue,
                    decoration: TextDecoration.underline,
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

【问题讨论】:

    标签: flutter dart flutter-dependencies flutter-test dart-pub


    【解决方案1】:

    这是因为 _formKey.currentState 从未被分配 - 为了分配它,您需要将密钥传递给您的表单小部件,如下所示:

    Form(
      key: _formKey,
      child: ...
    )
    

    然后表单小部件将负责分配currentState

    如果_formKey.currentState 为空,检查表单状态的方式也不会导致您的应用程序崩溃,这也是一个好主意。例如,在您的注册按钮中:

    onPressed: () {
      if (_formKey.currentState == null) {
        print("_formKey.currentState is null!");
      } else if (_formKey.currentState!.validate()) {
        print("Form input is valid");
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-19
      • 2021-11-17
      • 2021-07-08
      • 1970-01-01
      • 2022-07-20
      • 2021-08-04
      • 2022-09-30
      • 2021-11-10
      相关资源
      最近更新 更多