错误消息 - 未出现警报消息

问题描述

我在界面上放置了一条消息,向用户提供有关登录或注册错误的信息。

该错误未提供错误,应用程序继续运行。

但是,错误消息或状态不会传递给未出现的小部件 _showAlert。

    _signup() async {
    AuthNotifier authNotifier = Provider.of<AuthNotifier>(context,listen: false);
            {
      setState(() {
      });
      final status =
          await  signup(_user,authNotifier);
                if (status == AuthResultStatus.successful) {
                // Navigate to success screen
                Navigator.pushAndRemoveUntil(
          context,MaterialPageRoute(builder: (context) => SecondPage()),(r) => false);
      } else {
        final errorMsg = AuthExceptionHandler.generateExceptionMessage(status);
        _showAlert(errorMsg);
      }
  }
}

  _showAlert(errorMsg) {
    if (errorMsg != null) {
      return Container(
        color: Colors.amberAccent,width: double.infinity,padding: EdgeInsets.all(8.0),child: Row(
          children: <Widget>[
            Padding(
              padding: const EdgeInsets.only(right: 8.0),child: Icon(Icons.error_outline),),Expanded(
              child: AutoSizeText(
                errorMsg,maxLines: 3,Padding(
              padding: const EdgeInsets.only(left: 8.0),child: IconButton(
                icon: Icon(Icons.close),onPressed: () {
                  setState(() {
                    errorMsg = null;
                  });
                },)
          ],);
    }
    return SizedBox(
      height: 0,);
  }

解决方法

您正在返回一个未显示对话框的小部件。要显示一个对话框试试这个

_showAlert(errorMsg,BuildContext context) {
  if (errorMsg != null) {
    showDialog(
      context: context,builder: (BuildContext context) =>
          Dialog(
            child: Container(
              // ...
            ),),);
  }
}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...