我正在尝试创建一个简单的第一个应用程序,以一组TabBar选项卡为中心。我希望这些选项卡中的一个包含一个按钮,该按钮在单击时会启动模式,这似乎有必要将小部件推到导航堆栈中。

该选项卡的东西工作正常,但尝试推送模式窗口小部件会导致'package:flutter/src/widgets/navigator.dart': Failed assertion: line 2330 pos 12: '!_debugLocked': is not true.,该字段用于“防止重入调用推送,弹出和好友”。

关于此方法的任何事情看起来显然有问题吗?我将文本小部件替换为实际的模态,以尝试简化对错误源的跟踪。

class SessionsView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Timer(),
        ]);
  }
}

class Timer extends StatefulWidget {
  @override
  State<Timer> createState() => _Timer();
}

class _Timer extends State<Timer> {

  Duration timerDuration;

  @override
  void initState() {
    super.initState();
    timerDuration = Duration(seconds: 10);
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      children: <Widget>[
        TimerIconButton(),
      ],
    );
  }
}

class TimerIconButton extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return IconButton(
      onPressed: launchTestModal(context),
      alignment: Alignment.centerLeft,
      icon: Icon(Icons.timelapse),
    );
  }
}

launchTestModal(BuildContext context) {
  Navigator.push(
      context,
      MaterialPageRoute<Null>(
        builder: (BuildContext context) {
          return Text('something');
        },
        fullscreenDialog: true,
      ));
}

最佳答案

您可以在下面复制粘贴运行完整代码
您需要使用WidgetsBinding.instance.addPostFrameCallback
程式码片段

launchTestModal(BuildContext context) {
  WidgetsBinding.instance.addPostFrameCallback((_) {
    Navigator.push(
        context,
        MaterialPageRoute<Null>(
          builder: (BuildContext context) {
            return Text('something');
          },
          fullscreenDialog: true,
        ));
  });
}

工作演示

flutter - 在简单应用程序中调用Navigator.push()时,_debugLocked断言失败-LMLPHP

完整的代码
import 'package:flutter/material.dart';

class SessionsView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Timer(),
          ]),
    );
  }
}

class Timer extends StatefulWidget {
  @override
  State<Timer> createState() => _Timer();
}

class _Timer extends State<Timer> {
  Duration timerDuration;

  @override
  void initState() {
    super.initState();
    timerDuration = Duration(seconds: 10);
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      children: <Widget>[
        TimerIconButton(),
      ],
    );
  }
}

class TimerIconButton extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return IconButton(
      onPressed: launchTestModal(context),
      alignment: Alignment.centerLeft,
      icon: Icon(Icons.timelapse),
    );
  }
}

launchTestModal(BuildContext context) {
  WidgetsBinding.instance.addPostFrameCallback((_) {
    Navigator.push(
        context,
        MaterialPageRoute<Null>(
          builder: (BuildContext context) {
            return Text('something');
          },
          fullscreenDialog: true,
        ));
  });
}

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: SessionsView(),
    );
  }
}

关于flutter - 在简单应用程序中调用Navigator.push()时,_debugLocked断言失败,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60195811/

10-12 13:40