【问题标题】:Flutter: I got this error > Failed assertion: line 447 pos 12: 'context != null': is not trueFlutter:我收到此错误 > 断言失败:第 447 行 pos 12:'context != null': is not true
【发布时间】:2020-11-30 06:01:45
【问题描述】:

我有一个小部件,它返回一个 alertDialog,根据 http 请求的结果,我显示另一个 alertDialog。坚果我得到了以下错误:

[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] 未处理的异常:'package:flutter/src/widgets/localizations.dart':断言失败:第 447 行 pos 12:'context != null' : 不是真的。



import 'dart:io';

import 'package:Zabatnee/common_app/provider/user_details_provider.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class ForgetPasswordDialog extends StatefulWidget {
  static const routeName = '/customeDialog';
  final String title,description;
  

  ForgetPasswordDialog({
    this.title, 
    this.description,
    });

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

class _ForgetPasswordDialogState extends State<ForgetPasswordDialog> {
final emailController = TextEditingController();
  var _isLoading = false;

_showDialog(String title, String message) {
    showDialog(
      barrierDismissible: false,
      context: context,
      builder: (ctx) => WillPopScope(
        onWillPop: () async => false,
        child: new AlertDialog(
          elevation: 15,
          shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.all(Radius.circular(8))),
          title: Text(
            title,
            style: TextStyle(color: Colors.white),
          ),
          content: Text(
            message,
            style: TextStyle(color: Colors.white),
          ),
          backgroundColor: Theme.of(context).primaryColor,
          actions: <Widget>[
            FlatButton(
              child: Text(
                'OK',
                style: TextStyle(color: Theme.of(context).accentColor),
              ),
              onPressed: () {
                Navigator.of(context).pop();
                setState(
                  () {
                    _isLoading = false;
                  },
                );
              },
            )
          ],
        ),
      ),
    );
  }



  Future<void> _forgetPassword (String email) async {

    try{
      if(mounted)
     setState(() {
       _isLoading = true;
     }); 
    await Provider.of<UserDetailsProvider>(context, listen: false).forgetPassword(email);
      print('code are sent via email');
       _showDialog('Conguratulations', 'code send successfully');
    } on HttpException catch (error) {
      _showDialog('Authentication Failed', error.message);
    } on SocketException catch (_) {
      _showDialog('An error occured',
          'please check your internet connection and try again later');
    } catch (error) {
      _showDialog('Authentication Failed',
          'Something went wrong, please try again later');
    }
    if(mounted)
    setState(() {
      _isLoading = false;
    });
  }
  @override
  void dispose() {
    emailController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Dialog(
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(16)
      ),
      elevation: 8,
      backgroundColor: Theme.of(context).accentColor,
      child: dialogContent(context),
    );
  }

  dialogContent(BuildContext context){
    

    return Container(
      padding: EdgeInsets.only(
        top: 50,
        right: 16,
        left: 16,
        bottom: 16,
      ),
      margin: EdgeInsets.all(8),
      width: double.infinity,
      decoration: BoxDecoration(
        color: Theme.of(context).accentColor,
        shape: BoxShape.rectangle,
        borderRadius: BorderRadius.circular(17),
        boxShadow: [
          BoxShadow(
            color: Colors.black87,
            blurRadius: 10.0,
            offset: Offset(0.0,10.0),
          )
        ]
      ),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          Text(
            widget.title,
            style: TextStyle(
              fontSize: 24,
            ),
            ),
          SizedBox(
            height: 20,
          ),
          Text(widget.description, style: TextStyle(fontSize: 16),
          ),
          SizedBox(
            height: 20,
          ),
          TextField(
            controller: emailController,
            style: TextStyle(color: Colors.white),
            keyboardType: TextInputType.emailAddress,
            decoration: InputDecoration(
              border: OutlineInputBorder(
                borderSide: BorderSide(color:Theme.of(context).primaryColor)
              ),
              fillColor: Colors.black87,
              hintStyle: TextStyle(color:Colors.grey),
              hintText: 'please enter your email',
              labelText: 'Email',
              labelStyle: TextStyle(color:Colors.white)
            ),  
          ),
          SizedBox(
            height: 20,
          ),
          Container(
            width: double.infinity,
            child: RaisedButton(
              child: Text('Send'),
              onPressed: (){
                _isLoading 
                ? CircularProgressIndicator()
                : print(emailController.text);
                _forgetPassword(emailController.text);
                Navigator.of(context).pop();
              }
              ),
          ),
          Container(
            width: double.infinity,
            child: RaisedButton(
              child: Text('Cancel'),
              onPressed: (){
                Navigator.of(context).pop();
              }
              ),
          ),  
        ],
      ),
    );
  }
}

【问题讨论】:

    标签: flutter dart https android-alertdialog provider


    【解决方案1】:

    在你的 showDialog 中,你没有传递上下文(在参数中)。

    这样做,

    _showDialog(String title, String message, BuildContext context) {
        showDialog(
          barrierDismissible: false,
          context: context,
          builder: (ctx) => WillPopScope(
            onWillPop: () async => false,
            child: new AlertDialog(
              elevation: 15,
    
    )
    

    然后,在调用函数时,也要传递上下文,

    _showDialog('Conguratulations', 'code send successfully', context);
    

    【讨论】:

      猜你喜欢
      • 2021-03-07
      • 2020-06-05
      • 1970-01-01
      • 2020-11-24
      • 1970-01-01
      • 2020-03-01
      • 2023-01-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多