微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ScaffoldMessenger 不显示 SnackBar,而不是调试控制台中的问题

如何解决ScaffoldMessenger 不显示 SnackBar,而不是调试控制台中的问题

我正在尝试使用 Firebase 实现密码重置功能,如果发生 PlatformException 错误,则 SnackBar 会显示错误是什么。但是,尽管存在 PlatformException 错误,但 SnackBar 并未显示。但是,控制台没有显示 SnackBar 实现的任何问题。任何见解将不胜感激!

class ResetPasswordScreen extends StatefulWidget {
  @override
  _ResetPasswordState createState() => _ResetPasswordState();
}

class _ResetPasswordState extends State<ResetPasswordScreen> {
  final _auth = auth.FirebaseAuth.instance;
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
  String _email = '';
  @override
  Widget build(BuildContext context) {
    return Center(
      child: FractionallySizedBox(
        widthFactor: 1,heightFactor: .5,child: Padding(
          padding: const EdgeInsets.only(left: 10,right: 10),child: Card(
            color: Colors.white,shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(10),),child: Padding(
              padding: const EdgeInsets.all(10),child: Scaffold(
                resizetoAvoidBottomInset: false,backgroundColor: Colors.white,appBar: PreferredSize(
                  preferredSize: Size.fromHeight(40),child: AppBar(
                    backgroundColor: Colors.white,elevation: 0,leading: IconButton(
                      icon: Icon(Icons.arrow_back,color: Theme.of(context).primaryColor),onpressed: () => Navigator.of(context).pop(),title: Text(
                      'Password Reset',style: TextStyle(
                        color: Theme.of(context).primaryColor,fontSize: 28,body: Padding(
                  padding: EdgeInsets.all(20),child: Form(
                    key: _formKey,child: Column(
                      children: <Widget>[
                        resetEmail(context),SizedBox(height: 20),Align(
                          alignment: Alignment.bottomright,child: resetButton(),],);
  }

  TextFormField resetEmail(BuildContext context) {
    return TextFormField(
      keyboardType: TextInputType.emailAddress,cursorColor: Theme.of(context).accentColor,decoration: Inputdecoration(labelText: 'Email'),validator: (value) {
        if (value.isEmpty || !value.contains('@')) {
          return 'Please enter a valid email';
        }
        return null;
      },onSaved: (value) {
        _email = value.trim();
      },);
  }

  TextButton resetButton() {
    return TextButton(
      child: Text('Send request'),style: TextButton.styleFrom(
        primary: Theme.of(context).primaryColor,textStyle: TextStyle(
          fontSize: 20,onpressed: () => _submitRequest(),);
  }

  void _submitRequest() async {
    if (!_formKey.currentState.validate()) {
      return;
    }
    _formKey.currentState.save();
    FocusScope.of(context).unfocus();
    try {
      await _auth.sendPasswordResetEmail(email: _email);
      Navigator.of(context).pop();
    } on PlatformException catch (err) {
      var message = 'Please check your email';
      if (err.message != null) {
        message = err.message;
      }
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(
          content: Text(message),backgroundColor: Theme.of(context).errorColor,);
    } catch (err) {
      print(err);
    }
  }
}

解决方法

该问题与 Snackbar 代码无关(我对其进行了测试,并且运行正常)。问题可能是 showSnackBar 函数永远不会被调用。

,

事实证明这是一个上下文问题。我需要为 Scaffold 提供一个构建器函数,以便为小吃店提供适当的上下文。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?