gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 03:57:08 39 4
gpt4 key购买 nike

我正在尝试创建一个简单的第一个应用程序,以一组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,
));
});
}

工作演示

enter image description here

完整的代码
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/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com