“多个小部件使用相同的GlobalKey".颤振误差 [英] "Multiple widgets used the same GlobalKey" error in flutter

查看:68
本文介绍了“多个小部件使用相同的GlobalKey".颤振误差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了类似图片中的错误.我很困惑,因为我没有在每个页面上都设置 GlobalKey .我为此在 main.dart 上做了一个 GlobalKey :

I'm getting an error like the one in the picture. I'm confused because I'm not setting up GlobalKey on every page. I just made a GlobalKey on main.dart for this:

class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
  StreamController<bool> _showLockScreenStream = StreamController();
  StreamSubscription _showLockScreenSubs;
  GlobalKey<NavigatorState> _navigatorKey = GlobalKey();

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);

    _showLockScreenSubs = _showLockScreenStream.stream.listen((bool show){
      if (mounted && show) {
        _showLockScreenDialog();
      }
    });
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    _showLockScreenSubs?.cancel();
    super.dispose();
  }

  // Listen for when the app enter in background or foreground state.
  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    if (state == AppLifecycleState.resumed) {
      // user returned to our app, we push an event to the stream
      _showLockScreenStream.add(true);
    } else if (state == AppLifecycleState.inactive) {
      // app is inactive
    } else if (state == AppLifecycleState.paused) {
      // user is about quit our app temporally
    } else if (state == AppLifecycleState.suspending) {
      // app suspended (not used in iOS)
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      navigatorKey: _navigatorKey,
      ...
    );
  }

  void _showLockScreenDialog() {
    _navigatorKey.currentState.
        .pushReplacement(new MaterialPageRoute(builder: (BuildContext context) {
      return PassCodeScreen();
    }));
  }
}

我尝试删除 GlobalKey _navigatorKey ,但错误仍然出现.

I've tried to remove the GlobalKey _navigatorKey but the error still appears.

切换页面时出现错误.有谁可以帮助我吗?

The error appears when switching pages. Is there anyone who can help me?

推荐答案

Key 种类很多.但是 GlobalKey 允许访问小部件的状态(如果它是 StatefulWigdet ).

There are many kinds of Keys. But the GlobalKey allows access to the state of a widget (if it's a StatefulWigdet).

然后,如果您对其中许多使用相同的 GlobalKey ,则它们的 State 会冲突.

Then, if you use the same GlobalKey for many of them, there is a conflict with their States.

此外,由于其规格的原因,它们必须是同一类型:

In addition, they must be of the same type due to its specification:

abstract class GlobalKey<T extends State<StatefulWidget>> extends Key {
  // ...
  void _register(Element element) {
    assert(() {
      if (_registry.containsKey(this)) {
        assert(element.widget != null);
        final Element oldElement = _registry[this]!;
        assert(oldElement.widget != null);
        assert(element.widget.runtimeType != oldElement.widget.runtimeType);
        _debugIllFatedElements.add(oldElement);
      }
      return true;
    }());
    _registry[this] = element;
  }
  // ...
}

这段代码表明,在调试模式下,有一个断言可确保没有其他先前注册过的相同类型的 GlobalState .

This fragment of code shows that in debug mode, there is an assertion for ensuring that there isn't any other GlobalState of the same type previously registered.

这篇关于“多个小部件使用相同的GlobalKey".颤振误差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆