【问题标题】:The getter 'length' was called on null for my _showErrorMessage Widget我的 _showErrorMessage 小部件在 null 上调用了 getter 'length'
【发布时间】:2019-07-03 21:31:26
【问题描述】:

我尝试显示登录页面的错误消息,但是没有成功。 其他一切正常,但是当我想使用 _showErrorMessage() 方法时,应用程序会崩溃。

这是我所有涉及 _showErrorMessage() 方法的代码的一部分。

class LogInPage extends StatefulWidget {
LogInPage({this.auth, this.onSignedIn});
final BaseAuth auth;
final VoidCallback onSignedIn;


@override
_LogInPageState createState() => _LogInPageState();
}

enum FormMode { LOGIN, SIGNUP }

class _LogInPageState extends State<LogInPage> {
final _formKey = new GlobalKey<FormState>();

String _email;
String _password;
String _errorMessage = '';

FormMode _formMode = FormMode.LOGIN;
bool _isIos;
bool _isLoading;

// Check if form is valid before perform login or signup
bool _validateAndSave() {
final form = _formKey.currentState;
if (form.validate()) {
  form.save();
  return true;
  }
return false;
}

// Perform login or signup
void _validateAndSubmit() async {
setState(() {
  _errorMessage = "";
  _isLoading = true;
});
if (_validateAndSave()) {
  String userId = "";
  try {
    if (_formMode == FormMode.LOGIN) {
      userId = await widget.auth.signIn(_email, _password);
      print('Signed in: $userId');
    } else {
      userId = await widget.auth.signUp(_email, _password);
      widget.auth.sendEmailVerification();
      _showVerifyEmailSentDialog();
      print('Signed up user: $userId');
    }
    setState(() {
      _isLoading = false;
    });

    if (userId != null && userId.length > 0 && _formMode == FormMode.LOGIN) {
      widget.onSignedIn();
    }

  } catch (e) {
    print('Error: $e');
    setState(() {
      _isLoading = false;
      if (_isIos) {
        _errorMessage = e.details;
      } else
        _errorMessage = e.message;
    });
    }
  }
}


@override
void initState() {
_errorMessage = "";
_isLoading = false;
super.initState();
 }

void _changeFormToSignUp() {
_formKey.currentState.reset();
_errorMessage = "";
setState(() {
  _formMode = FormMode.SIGNUP;
  });
 }

 void _changeFormToLogin() {
_formKey.currentState.reset();
_errorMessage = "";
setState(() {
  _formMode = FormMode.LOGIN;
});
}


@override
Widget build(BuildContext context) {
_isIos = Theme.of(context).platform == TargetPlatform.iOS;
return new Scaffold(
    appBar: new AppBar(
      title: new Text("Organizer"),
      backgroundColor: Colors.blueAccent,
    ),
    body: Stack(
      children: <Widget>[
        _showBody(),
        _showCircularProgress(),
      ],
    ));
    }



  Widget _showBody(){
   return new Container(
    padding: EdgeInsets.all(16.0),
    child: new Form(
      key: _formKey,
      child: new ListView(
        shrinkWrap: true,
        children: <Widget>[
          _showEmailInput(),
          _showPasswordInput(),
          _showPrimaryButton(),
          _showSecondaryButton(),
          _showErrorMessage(),
        ],
      ),
    ));
     }
    Widget _showErrorMessage() {
    if (_errorMessage.length > 0 && _errorMessage != null) {
   return new Text(
    _errorMessage,
    style: TextStyle(
        fontSize: 13.0,
        color: Colors.red,
        height: 1.0,
        fontWeight: FontWeight.w300),
  );
} else {
  return new Container(
    height: 0.0,
  );
 }
}

我了解此错误的含义,但我不知道如何解决。 请帮助我,非常感谢:)

【问题讨论】:

    标签: firebase flutter dart firebase-authentication


    【解决方案1】:

    更改此代码

     if (_errorMessage.length > 0 && _errorMessage != null) 
    

    到这里:

     if (_errorMessage != null && _errorMessage.length > 0) 
    

    说明
    当对象为 null 时,没有可访问的属性。您首先检查了长度属性,即使对象为空。首先移动空检查确保不会访问空对象的属性。

    【讨论】:

    • 谢谢,这真的很有帮助。我能知道它为什么会这样吗?
    • 乐于助人:)。用解释更新了答案
    猜你喜欢
    • 2021-07-21
    • 2021-08-11
    • 2021-02-23
    • 2021-12-10
    • 2018-12-03
    • 2020-06-15
    • 2021-05-25
    • 2023-03-08
    相关资源
    最近更新 更多