我正在使用Getx状态管理。我有一个LOGINSCREEN和它的GetxController。在该GetxController中,我定义了一个类似于这样的FormKey:final formKey = GlobalKey<FormState>();
当我从任何其他屏幕直接导航回到LOGINSCREEN(用于注销)时,我遇到了此问题:Get.offAllNamed(Routes.loginScreen);
我尝试过flutter clean
,但它不起作用。我似乎找不到解决方法。
如果有人能找到解决方案,那将是非常有帮助的。
he following assertion was thrown building Form-[LabeledGlobalKey<FormState>#f1349](state: FormState#45516):
'package:flutter/src/widgets/will_pop_scope.dart': Failed assertion: line 135 pos 12: '_route == ModalRoute.of(context)': is not true.
2
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=2_bug.md
The relevant error-causing widget was
Form-[LabeledGlobalKey<FormState>#f1349]
lib\…\login_screen\login_screen.dart:40
When the exception was thrown, this was the stack
#2 _WillPopScopeState.didUpdateWidget
package:flutter/…/widgets/will_pop_scope.dart:135
#3 StatefulElement.update
package:flutter/…/widgets/framework.dart:4682
#4 Element.updateChild
package:flutter/…/widgets/framework.dart:3293
#5 ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:4520
#6 StatefulElement.performRebuild
package:flutter/…/widgets/framework.dart:4667
登录界面
class LoginScreen extends StatelessWidget {
final controller = Get.find<AuthController>();
@override
Widget build(BuildContext context) {
return Column(
children: [
Form(
key: controller.formKey,
child: Column(
children: [
TextFormField(
controller: controller.phoneController,
keyboardType: TextInputType.phone,
style: TextStyles.black14,
decoration: InputDecoration(
hintText: 'Phone Number',
hintStyle: TextStyles.hintStyle14,),
validator: (value) {
print(value);
if (value.length != 10) {
return 'Invalid phone number';
}
return null;
},
),
TextButton(
onPressed: () {
controller.login();
},
child: Text('Login'))
],
);
}
}
控制器
import 'package:app/routing/routes.dart';
import 'package:app/utilities/shared_prefs.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
class AuthController extends GetxController {
//Handling loading state
bool loading = false;
final formKey = GlobalKey<FormState>();
login() async {
if (!formKey.currentState.validate()) return;
loading = true;
update();
//API CALL
}
//Sign out user
signOut() async {
SharedPrefs().clear();
Get.offAllNamed(Routes.loginScreen);
}
}
以下是流程: 登录界面 --> 主页面 --> 其他界面
从其他界面调用controller.signOut()
会导致此错误。