【问题标题】:Flutter Form: The method 'validate' was called on null. [All configurations are already configured]Flutter Form:在 null 上调用了方法“validate”。 【所有配置都已经配置好了】
【发布时间】:2020-07-04 22:16:28
【问题描述】:

请帮助我!我正在创建简单的登录页面,但出现如下异常:

══╡ 小部件库发现异常╞════════════════ [+3s] I/flutter (11275): 抛出以下 NoSuchMethodError 构建登录屏幕(脏,依赖项:[MediaQuery]):[] I/flutter (11275):在 null 上调用了方法“验证”。 [
] I/flutter (11275):接收器:空 [] I/flutter (11275): 尝试调用: validate() [ ] I/flutter (11275): [ ] I/flutter (11275):相关的导致错误的小部件是:[] 我/颤动(11275):登录屏幕

虽然我想,我已经配置了所有需要的配置。但不知何故,我的代码抛出了异常。如何解决?

这是我的代码:

import 'package:flutter/material.dart';
import 'package:flutter_icons/flutter_icons.dart';
import 'package:my_app/services/UserService.dart';

/*
 * This is login screen view.
 * 
 * If you want to edit form section for example email and password fields, just edit these below functions.
 * - _formEmailWidget()
 * - _formPasswordWidget()
 * If you want to edit social login section, just edit _socialLoginWidget() function.
 * If you want to edit or change logo, just edit _logoWidget() function.
 */
class LoginScreen extends StatelessWidget {
  static final Pattern _pattern =
      r'^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$';
  final GlobalKey<FormState> _key = GlobalKey<FormState>();
  final TextEditingController _emailController = TextEditingController();
  final TextEditingController _passwordController = TextEditingController();

  final UserAuth _auth = UserAuth.instance();

  final Status status;

  LoginScreen({@required this.status});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        resizeToAvoidBottomPadding: false,
        body: SafeArea(child: this._bodyWidgets(context)));
  }

  _logoWidget() {
    return Padding(
        padding: const EdgeInsets.only(top: 15.0, bottom: 50.0),
        child: Image(image: AssetImage('assets/images/logo.png')));
  }

  _formEmailWidget() {
    return Container(
        decoration: BoxDecoration(
            border:
                Border(bottom: BorderSide(color: Colors.white, width: 1.0))),
        child: TextFormField(
          controller: _emailController,
          keyboardType: TextInputType.emailAddress,
          validator: (String value) {
            RegExp regex = new RegExp(_pattern);
            if (regex.hasMatch(value)) {
              return null;
            }
            return "Please, Enter valid e-mail";
          },
          decoration: const InputDecoration(
              icon: Icon(
                FontAwesome.user,
                color: Colors.white,
              ),
              fillColor: Colors.white,
              hintStyle: TextStyle(
                color: Colors.white,
                fontFamily: 'ProximaThin',
              ),
              hintText: 'Your e-mail'),
        ));
  }

  _formPasswordWidget() {
    return Container(
        decoration: BoxDecoration(
            border:
                Border(bottom: BorderSide(color: Colors.white, width: 1.0))),
        child: TextFormField(
          controller: _passwordController,
          validator: (String value) {
            if (value.isEmpty || value.length < 6) {
              return "Password must be more than 6 characters";
            }
            return null;
          },
          decoration: const InputDecoration(
              icon: Icon(
                FontAwesome.key,
                color: Colors.white,
              ),
              fillColor: Colors.white,
              hintStyle: TextStyle(
                color: Colors.white,
                fontFamily: 'ProximaThin',
              ),
              hintText: 'Your password'),
          obscureText: true,
        ));
  }

  _submitRowWidget() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: <Widget>[
        InkWell(
          onTap: null,
          child: Text(
            'forget password?',
            style: TextStyle(color: Colors.white),
          ),
        ),
        OutlineButton(
          textColor: Colors.white,
          highlightedBorderColor: Colors.white,
          borderSide: BorderSide(
              color: Colors.white, width: 0.8, style: BorderStyle.solid),
          shape: OutlineInputBorder(borderRadius: BorderRadius.circular(10.0)),
          onPressed: _loginAction(),
          child: Text(
            'Login',
            style: TextStyle(color: Colors.white),
          ),
        )
      ],
    );
  }

  _loginAction() {
    if (_key.currentState.validate()) {
      _auth.signIn(_emailController.text, _passwordController.text);
    } else {
      AlertDialog(
        content: Text(
            "Your e-mail or password doesn't match, Would you like create an account?"),
        elevation: 24.0,
        shape: OutlineInputBorder(borderRadius: BorderRadius.circular(10)),
        actions: <Widget>[
          FlatButton(
            onPressed: null,
            child: Text("Yes"),
          )
        ],
      );
    }
  }

  _socialLoginWidget() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: <Widget>[
        Container(
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.all(Radius.circular(10)),
          ),
          child: FlatButton(
            padding: const EdgeInsets.only(
                left: 10.0, top: 0.0, bottom: 0.0, right: 10.0),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                Icon(FontAwesome.facebook_f, color: Colors.black),
                SizedBox(
                  width: 10,
                ),
                Text('Facebook',
                    style: TextStyle(
                        fontFamily: 'Proxima',
                        fontSize: 11,
                        color: Colors.black))
              ],
            ),
            onPressed: null,
          ),
        ),
        Container(
          width: 100,
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.all(Radius.circular(10)),
          ),
          child: FlatButton(
            padding: const EdgeInsets.only(
                left: 10.0, top: 0.0, bottom: 0.0, right: 10.0),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                Icon(
                  FontAwesome.google,
                  color: Colors.black,
                ),
                SizedBox(
                  width: 10,
                ),
                Text(
                  'Google',
                  style: TextStyle(
                      fontFamily: 'Proxima', fontSize: 11, color: Colors.black),
                )
              ],
            ),
            onPressed: null,
          ),
        )
      ],
    );
  }

  _bodyWidgets(BuildContext context) {
    return Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          _logoWidget(),
          Expanded(
              child: Center(
            child: Container(
              width: MediaQuery.of(context).size.width - 20,
              decoration: BoxDecoration(
                  color: Colors.black,
                  boxShadow: [
                    BoxShadow(
                        color: Color.fromRGBO(0, 0, 0, 0.35),
                        blurRadius: 15.0,
                        offset: Offset(0, -5))
                  ],
                  borderRadius: BorderRadius.only(
                      topLeft: Radius.circular(30),
                      topRight: Radius.circular(30))),
              child: Padding(
                padding:
                    const EdgeInsets.only(top: 45.0, left: 45.0, right: 45.0),
                child: Column(children: <Widget>[
                  Form(
                    key: this._key,
                    child: Column(
                      children: <Widget>[
                        _formEmailWidget(),
                        _formPasswordWidget(),
                        SizedBox(
                          height: 15.0,
                        ),
                        _submitRowWidget()
                      ],
                    ),
                  ),
                  SizedBox(
                    height: 45,
                  ),
                  Text('or connect using',
                      style: TextStyle(
                          fontFamily: 'ProximaThin', color: Colors.white)),
                  SizedBox(
                    height: 15,
                  ),
                  _socialLoginWidget(),
                  SizedBox(
                    height: 15,
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      Text(
                        'Don\'t have an account?',
                        style: TextStyle(
                            fontFamily: 'ProximaThin', color: Colors.white),
                      ),
                      SizedBox(
                        width: 5,
                      ),
                      InkWell(
                        onTap: null,
                        child: Text('Sign up',
                            style: TextStyle(
                                fontFamily: 'Proxima', color: Colors.white)),
                      )
                    ],
                  )
                ]),
              ),
            ),
          ))
        ]);
  }
}

【问题讨论】:

    标签: android flutter dart mobile


    【解决方案1】:

    你有两个选择:

    1. 将下面的方法OnPressed: _loginAction(),改为OnPressed: _loginAction,

    2. 或将同一行改为OnPressed: () { _loginAction(); },

    固定代码如下:

      _submitRowWidget() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: <Widget>[
        InkWell(
          onTap: null,
          child: Text(
            'forget password?',
            style: TextStyle(color: Colors.white),
          ),
        ),
        OutlineButton(
          textColor: Colors.white,
          highlightedBorderColor: Colors.white,
          borderSide: BorderSide(
              color: Colors.white, width: 0.8, style: BorderStyle.solid),
          shape: OutlineInputBorder(borderRadius: BorderRadius.circular(10.0)),
          onPressed: _loginAction,
          child: Text(
            'Login',
            style: TextStyle(color: Colors.white),
          ),
        )
      ],
    );
    

    }

    【讨论】:

      猜你喜欢
      • 2020-11-06
      • 2019-09-23
      • 2020-06-04
      • 2021-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-25
      • 2021-06-21
      相关资源
      最近更新 更多