首页 > 解决方案 > 为什么showcaseview在第一次尝试时会抛出错误?

问题描述

嘿,我在 Flutterapp 中使用 ShowCaseView。当我第一次打开我的应用程序时,注册后没有展示,甚至没有显示应该“展示”的图标。但是当我关闭应用程序并再次打开它时,它工作得很好。

那是指展示的代码:

class _HomeSearchPageState extends State<HomeSearchPage> {

  final keyOne = GlobalKey();


  void initState() {
    super.initState();

  WidgetsBinding.instance.addPostFrameCallback(
    (_) => ShowCaseWidget.of(context).startShowCase([
      keyOne,
    ]),
  );

}

现在是脚手架:

    return Scaffold(
    appBar: PreferredSize(
      preferredSize: Size.fromHeight(getTopBarSize()),
      child: AppBar(
        automaticallyImplyLeading: false,
        title: Text(
          'username',
          style: TextStyle(fontSize: 14),
        ),
        actions: <Widget>[
          Showcase(
            key: keyOne,
            description: 'test',
            child: IconButton(
                icon: Icon(Icons.search),
                onPressed: () {
                  showSearch(context: context, delegate: DataSearch())
                      .whenComplete(() => setName());
                }),
          ),
        ],
      ),
    ),)

我做了与 Github 示例完全相同的操作,但它仍然在第一次抛出此错误:

    ════════ Exception caught by widgets library ═══════════════════════════════════
The following NoSuchMethodError was thrown building IconTheme(color: Color(0xffffffff)):
The getter 'activeWidgetIds' was called on null.
Receiver: null
Tried calling: activeWidgetIds

    The relevant error-causing widget was
AppBar
lib\Views\HomeSearchPage.dart:182
When the exception was thrown, this was the stack
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
#1      ShowCaseWidget.activeTargetWidget
package:showcaseview/showcase_widget.dart:51
#2      _ShowcaseState.showOverlay
package:showcaseview/showcase.dart:171
#3      _ShowcaseState.didChangeDependencies
package:showcaseview/showcase.dart:164
#4      StatefulElement._firstBuild
package:flutter/…/widgets/framework.dart:4786

════════ Exception caught by rendering library ═════════════════════════════════
Each child must be laid out exactly once.
The relevant error-causing widget was
AppBar
lib\Views\HomeSearchPage.dart:182
════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by scheduler library ═════════════════════════════════
Exception: Please provide ShowCaseView context



 ════════ Exception caught by widgets library ═══════════════════════════════════
'package:flutter/src/widgets/framework.dart': Failed assertion: line 6224 pos 12: '_children.contains(child)': is not true.
The relevant error-causing widget was
AppBar
lib\Views\HomeSearchPage.dart:182
════════════════════════════════════════════════════════════════════════════════

这些错误是一种循环,因此调试控制台一直显示它们。第一个应用程序打开和第二个应用程序之间的唯一区别是:在第一个应用程序中,用户进入一个注册页面,然后他进入带有展示视图的页面。在第二个中,用户直接进入带有展示视图的页面。这如何影响展示柜,更准确地说为什么会引发错误?

标签: flutter

解决方案


不确定我们是否有类似的问题,但也许我的回答会以某种方式帮助你:)。当我导航到拥有 Showcase 小部件的页面时会出现此问题,但当我从其他页面导航“返回”到该页面时不会出现此问题。

也许您可以查看此页面“如果您没有使用 ShowCaseWidget 包装小部件,则会引发此错误。这不是包本身的问题。”

因此,我尝试在 main.dart 中提高 ShowCaseWidget 的级别,问题就解决了。

void main() {
  // runApp(MyApp());
  runApp(
    ShowCaseWidget(
      autoPlay: false,
      autoPlayDelay: Duration(seconds: 8),
      autoPlayLockEnable: false,
      builder: Builder(
        builder: (context) => MyApp(),
      ),
      onStart: (index, key) {
        print('onStart: $index, $key');
      },
      onComplete: (index, key) {
        print('onComplete: $index, $key');
      },
    ),
  );
}

到目前为止,该应用程序仍然运行良好。如果出现任何问题,我会在这里再次报告。


推荐阅读