【问题标题】:Failed assertion: line 61 pos 12: '_route == ModalRoute.of(context)': is not true断言失败:第 61 行 pos 12:'_route == ModalRoute.of(context)':不正确
【发布时间】:2021-03-08 14:20:43
【问题描述】:

当我打开我的应用程序到登录屏幕时,屏幕工作正常,但事情并非如此, 现在,当我单击注册按钮并尝试单击返回登录时,我收到此错误

    ======== Exception caught by widgets library =======================================================
The following assertion was thrown building Form-[LabeledGlobalKey<FormState>#d062b](state: FormState#ba10e):
'package:flutter/src/widgets/will_pop_scope.dart': Failed assertion: line 61 pos 12: '_route == ModalRoute.of(context)': is not true.


Either the assertion indicates an error in the framework itself, or we should provide substantially more information in this error message to help you determine and fix the underlying cause.
In either case, please report this assertion by filing a bug on GitHub:
  https://github.com/flutter/flutter/issues/new?template=BUG.md

The relevant error-causing widget was: 
  Form-[LabeledGlobalKey<FormState>#d062b] file:///C:/Users/Dell%20Latitude/AndroidStudioProjects/handyman_client/lib/screens/home_page.dart:61:65
When the exception was thrown, this was the stack: 
#2      _WillPopScopeState.didUpdateWidget (package:flutter/src/widgets/will_pop_scope.dart:61:12)
#3      StatefulElement.update (package:flutter/src/widgets/framework.dart:4815:58)
#4      Element.updateChild (package:flutter/src/widgets/framework.dart:3314:15)
#5      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4652:16)
#6      StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4800:11)
...
====================================================================================================

======== Exception caught by widgets library =======================================================
Duplicate GlobalKey detected in widget tree.
====================================================================================================

查看后我发现它来自我的表单小部件 这是我的代码: 但请注意表单小部件

    import 'package:auto_size_text/auto_size_text.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:handyman_client/models/login_model.dart';
import 'package:provider/provider.dart';

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SingleChildScrollView(
        child: Container(
            margin: EdgeInsets.only(top: 50),
            child: Column(
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Image.asset("assests/images/onboarding1.png", width: 80, height: 80,),
                      Container(
                          margin: EdgeInsets.only(top: 50),
                          child: AutoSizeText(
                            "HANDYMAN",
                            style: TextStyle(
                                color: Color(0xFF2AF219),
                                fontSize: 14,
                                fontFamily: 'Ramabhadra',
                                fontWeight: FontWeight.w900
                            ),
                          )
                      )
                    ],
                  ),
                  SizedBox(height: 20,),
                  Container(
                    padding: EdgeInsets.symmetric(horizontal: 20),
                    child: Consumer<LoginModel>(
                      builder: (context, mLoginValue, child) => Form(
                        key: mLoginValue.formKey,
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            AutoSizeText(
                              "Hire skilled handy workers that lives in you area",
                              maxLines: 3,
                              style: TextStyle(
                                  color: Color(0xFF2AF219),
                                  fontSize: 25,
                                  fontFamily: 'Ramabhadra',
                                  fontWeight: FontWeight.w900
                              ),
                            ),
                            SizedBox(height: 12,),
                            AutoSizeText(
                              "Sign In",
                              maxLines: 2,
                              style: TextStyle(
                                  color: Color(0xFFC4C4C4),
                                  fontFamily: 'Ramabhadra',
                                  fontSize: 24
                              ),
                            ),
                            SizedBox(height: 10,),
                            AutoSizeText(
                              "Email",
                              style: TextStyle(
                                color: Color(0xFF2AF219),
                                fontFamily: 'Ramabhadra',
                              ),
                            ),
                            SizedBox(height: 10,),
                            Container(
                              padding: EdgeInsets.only(left: 16.0),
                              height: 50,
                              decoration: BoxDecoration(
                                  color: Colors.white,
                                  borderRadius: BorderRadius.circular(16.0),
                                  boxShadow: [
                                    BoxShadow(
                                        color: Color.fromRGBO(0, 0, 0, 0.1),
                                        offset: Offset(6, 2),
                                        blurRadius: 6.0,
                                        spreadRadius: 3.0
                                    )
                                  ]
                              ),
                              child: TextFormField(
                                validator: (v) => mLoginValue.validateEmail(v),
                                controller: mLoginValue.emailController,
                                keyboardType: TextInputType.emailAddress,
                                decoration: InputDecoration(
                                    border: InputBorder.none,
                                    hintText: "email@mail.com",
                                    hintStyle: TextStyle(
                                      color: Color(0xFFC4C4C4),
                                      fontFamily: 'Ramabhadra',
                                    )
                                ),
                              ),
                            ),
                            SizedBox(height: 12,),
                            AutoSizeText(
                              "Password",
                              style: TextStyle(
                                color: Color(0xFF2AF219),
                                fontFamily: 'Ramabhadra',
                              ),
                            ),
                            SizedBox(height: 10,),
                            Container(
                              padding: EdgeInsets.only(left: 16.0),
                              height: 50,
                              decoration: BoxDecoration(
                                  color: Colors.white,
                                  borderRadius: BorderRadius.circular(16.0),
                                  boxShadow: [
                                    BoxShadow(
                                        color: Color.fromRGBO(0, 0, 0, 0.1),
                                        offset: Offset(6, 2),
                                        blurRadius: 6.0,
                                        spreadRadius: 3.0
                                    )
                                  ]
                              ),
                              child: TextFormField(
                                controller: mLoginValue.passwordController,
                                validator: (v) => mLoginValue.validatePassword(v),
                                obscureText: true,
                                decoration: InputDecoration(
                                    border: InputBorder.none,
                                    hintText: "password",
                                    hintStyle: TextStyle(
                                      color: Color(0xFFC4C4C4),
                                      fontFamily: 'Ramabhadra',
                                    )
                                ),
                              ),
                            ),
                          ],
                        ),
                      ),
                    ),
                  ),
                  SizedBox(height: 15,),
                  Container(
                    margin: EdgeInsets.only(left: 180),
                    child: FlatButton(
                      onPressed: (){
                        Navigator.pushNamed(context, '/forgotPassword');
                      },
                      child: AutoSizeText(
                        "Forgot Password",
                        style: TextStyle(
                          color: Color(0xFFC4C4C4),
                          fontFamily: 'Ramabhadra',
                        ),
                      ),
                    ),
                  ),
                  SizedBox(height: 20,),
                  Container(
                    child:  Consumer<LoginModel>(
                      builder: (context, mLoginValue, child) => InkWell(
                        onTap: () async {
                          mLoginValue.createLogin();
                          if(mLoginValue.isVerified){
                            Navigator.pushNamed(context, '/profileImageScreen');
                          }
                        },
                        child: CircleAvatar(
                          radius: 28.93,
                          backgroundColor: Colors.black,
                          foregroundColor: Colors.white,
                          child: Icon(Icons.arrow_forward),
                        ),
                      ),
                    ),
                  ),

                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      AutoSizeText(
                        "Don't have account?",
                        style: TextStyle(
                            color: Color(0xFF464444),
                            fontFamily: 'Ramabhadra',
                            fontSize: 11
                        ),
                      ),
                      FlatButton(
                          onPressed: (){
                            Navigator.pushNamed(context, '/clientSignUp');
                          },
                          child: AutoSizeText(
                            "Click Here",
                            style: TextStyle(
                                color: Color(0xFF2AF219),
                                fontFamily: 'Ramabhadra',
                                fontSize: 11,
                                fontWeight: FontWeight.bold
                            ),
                          )
                      )
                    ],
                  ),
                  Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      AutoSizeText(
                        "For more information visit",
                        style: TextStyle(
                            color: Color(0xFF464444),
                            fontFamily: 'Ramabhadra',
                            fontSize: 11
                        ),
                      ),
                      SizedBox(height: 50,),
                      FlatButton(
                          onPressed: (){},
                          child: AutoSizeText(
                            "www.handyman.com",
                            style: TextStyle(
                                color: Color(0xFF2AF219),
                                fontFamily: 'Ramabhadra',
                                fontSize: 11,
                                fontWeight: FontWeight.bold
                            ),
                          )
                      )
                    ],
                  )
                ]
            )
        ),
      ),
    );
  }
}

我也使用了提供者 这是代码:

import 'package:flutter/cupertino.dart';
import 'package:handyman_client/signUp_Validation/validate.dart';

class LoginModel extends ChangeNotifier with ValidationMixin {
  TextEditingController emailController = TextEditingController();
  TextEditingController passwordController = TextEditingController();

  final formKey = GlobalKey<FormState>();

  String _message = "";
  bool _isVerified = false;

  set message(msg) {
    _message = msg;
    notifyListeners();
  }
  get message {
    return this._message;
  }

  set isVerified(bool verified) {
    _isVerified = verified;
    notifyListeners();
  }

  get isVerified {
    return this._isVerified;
  }

  Future<void> createLogin() {
    if (validateForm()) {
      emailController.text;
      passwordController.text;
      isVerified = true;
    }else {
      message = "Please fill the form";
      isVerified = false;
    }
    notifyListeners();
  }

  bool validateForm() {
    return formKey.currentState.validate();
  }
}

请问我该如何解决?

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    您有两个具有相同密钥的小部件 尝试改变最终的 formKey = GlobalKey(); 到 formKey = GlobalKey();并尝试在它自己的小部件中初始化它并通过 formState 来更改通知器

    
    class LoginModel extends ChangeNotifier with ValidationMixin {
        /// remove this line 
        final formKey = GlobalKey<FormState>();
        
        //add form key as parameter
        Future<void> createLogin(GlobalKey<FormState> formKey) {
        if (validateForm(formKey)) {
        ///...
        }else {
         
        }
        notifyListeners();
      }
      bool validateForm(GlobalKey<FormState> formKey) {
        return formKey.currentState.validate();
      }
    }
    
    /// in Home Widget init form key
     GlobalKey<FormState> formKey
    
      @override
      void initState() {
        super.initState();
       formKey = GlobalKey<FormState>();
      }
    
    

    【讨论】:

    • 对不起,你能解释更多或把它放在代码的形式吗?请
    • 我更新了我的答案,在阅读更多内容后,您编写了我对您的建议,无需与提供商一起创建您的表单
    猜你喜欢
    • 2021-07-24
    • 2021-02-23
    • 2021-08-07
    • 2020-04-22
    • 2020-12-29
    • 2021-11-02
    • 2021-05-30
    • 2020-03-01
    • 1970-01-01
    相关资源
    最近更新 更多