失败的断言:第 61 行 pos 12:"_route == ModalRoute.of(context)":不为 真



当我打开我的应用程序到登录屏幕时,屏幕工作正常,没有事情发生,现在,当我点击注册按钮,我试图点击返回登录,我得到这个错误

======== 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();
}
}

请问如何解决这个问题?

您有两个具有相同键的小部件尝试改变最终formKey = GlobalKey();to formKey = GlobalKey();并尝试在widget it self中初始化它并将formState设置为changeNotifier


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>();
}

最新更新