【问题标题】:'package:flutter/src/widgets/will_pop_scope.dart': Failed assertion: line 135 pos 12: '_route == ModalRoute.of(context)': is not true'package:flutter/src/widgets/will_pop_scope.dart':断言失败:第 135 行 pos 12:'_route == ModalRoute.of(context)':不正确
【发布时间】:2021-08-07 09:44:52
【问题描述】:

我正在使用 Getx 状态管理。 我有一个 LOGINSCREEN 和它的 GetxController。在那个 GetxController 中,我定义了一个这样的 FormKey final formKey = GlobalKey<FormState>();

当我从任何其他屏幕使用 Get.offAllNamed(Routes.loginScreen); 直接导航回 LOGINSCREEN (For SignOut) 时,我遇到了这个问题。

我试过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);
  }
}

这是流程。 登录屏幕 --> 主屏幕 --> 其他屏幕

从 OtherScreen 调用 controller.signOut() 会导致此错误

【问题讨论】:

  • 请分享您的代码
  • 已添加,请查看是否可以提供帮助
  • 您好,您可以通过将formkey移动到登录页面来解决问题。
  • 是的,我将 formKey 移至登录页面并将其转换为 StatefulWidget。它有效。谢谢大佬!
  • 我猜你不需要有状态的小部件。请将我的回答标记为已接受。

标签: android flutter flutter-getx


【解决方案1】:

如果尝试重置全局密钥,则会发生这种情况。

要解决此问题,您可以将 GlobalKey 和 TextEditingController 移动到页面本身,而不是在控制器中声明它们。

class LoginScreen extends StatelessWidget {
  
   final formKey = GlobalKey<FormState>();

   TextEditingController phoneController = TextEditingController();


  final controller = Get.find<AuthController>();
  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
Form(
                    key:formKey,
                    child: Column(
                      children: [
                        TextFormField(
                          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: () {
           //Validate here
           if (!formKey.currentState!.validate()) return;
            controller.login();
          },
          child: Text('Login'))
],
    );
  }
}

【讨论】:

  • 没有办法把formKey保存在Controller里面?我有很多逻辑。
  • 非常感谢,它成功了。我只是在视图上实例化,所以我通过函数参数发送给控制器进行验证。
猜你喜欢
  • 2021-07-24
  • 1970-01-01
  • 1970-01-01
  • 2020-03-01
  • 2022-06-27
  • 2021-02-23
  • 2021-04-12
  • 2021-02-05
  • 2021-01-16
相关资源
最近更新 更多