简体   繁体   English

颤振错误状态:未来已完成且断言失败:第 2113 行 pos 12:'!_debugLocked':不正确

[英]flutter Bad state: Future already completed and Failed assertion: line 2113 pos 12: '!_debugLocked': is not true

First post on this site so if there are any tips on formatting the question or not following the guidelines please let me know.第一次在这个网站上发帖,所以如果有关于格式化问题或不遵循指南的任何提示,请告诉我。 Thank you谢谢

In the flutter app I am creating I am using a third party calendar widget that is nested inside a modalBottomSheet.在我创建的 flutter 应用程序中,我使用的是嵌套在 modalBottomSheet 中的第三方日历小部件。 When using Navigator.pop in the onChanged method to close the calendar widget, the following error is thrown:在 onChanged 方法中使用 Navigator.pop 关闭日历小部件时,抛出以下错误:

'package:flutter/src/widgets/navigator.dart': Failed assertion: line 2330 pos 12: '!_debugLocked': is not true. 'package:flutter/src/widgets/navigator.dart':断言失败:第 2330 行 pos 12:'!_debugLocked':不正确。

If I pick a day in the same month that the calendar is created, I do not get an error.如果我在创建日历的同一个月选择一天,我不会收到错误消息。 The error only appears if I go backwards or forwards a month and select a date.只有当我向后或向前一个月并选择一个日期时才会出现错误。 I also get the error when I move forward (or backward) a month and then come back to the month that the calendar was created in. For example, if I use today's date as the selected date, the month is March.当我向前(或向后)一个月移动然后返回到创建日历的月份时,我也会收到错误消息。例如,如果我使用今天的日期作为所选日期,则该月份是三月。 If I use the arrows and go back to February or forward to April and select a date, I get the above error.如果我使用箭头返回二月或四月并选择一个日期,则会出现上述错误。

The calendar package I am using is: flutter_date_pickers: ^0.0.5我使用的日历包是:flutter_date_pickers: ^0.0.5

I have created a minimum running example from the code below:我从下面的代码创建了一个最小运行示例:

import 'package:flutter/material.dart';
import 'package:flutter_date_pickers/flutter_date_pickers.dart' as dp;

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String date;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    date = DateTime.now().toIso8601String();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'Selected Date:',
            ),
            Text(
              '$date',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          _openDatePicker(context);
        },
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }

  void _openDatePicker(BuildContext context) {
    showModalBottomSheet<DateTime>(
        context: context,
        builder: (BuildContext context) {
          return Container(
            child: dp.DayPicker(
              selectedDate: DateTime.now(),
              onChanged: (dateTime) {
                Navigator.pop(context, dateTime);
              },
              firstDate: DateTime.now().subtract(Duration(days: 360)),
              lastDate: DateTime.now().add(Duration(days: 360)),
            ),
          );
        }).then(
      (dateTime) {
        if (dateTime != null) {
          setState(() {
            date = dateTime.toIso8601String();
          });
          print('date time: ' + date);
        } else {
          print('No Day Selected');
        }
      },
    );
  }
}

I have gone through and looked at other questions with similar errors and tried all of the different methods to fix this with only one method working for me.我已经浏览并查看了其他有类似错误的问题,并尝试了所有不同的方法来解决这个问题,只有一种方法对我有用。 It was to convert the Navigator.pop to a Navigator.pushNamedAndReplace method.它将 Navigator.pop 转换为 Navigator.pushNamedAndReplace 方法。 Unfortunately in my actual app this becomes so slow that it is not realistically usable.不幸的是,在我的实际应用程序中,这变得如此缓慢,以至于实际上无法使用。

Any help would be appreciated.任何帮助,将不胜感激。 Thank you谢谢

As the print inside onChanged shows for some reason onChanged is called twice every time user selects date, this causes the overlapping of multiple Navigator.pop()s leading to debugLocked issue , what I did is simply making sure Navigator.pop() is only getting called once using a boolean variable like this由于 onChanged 中的打印显示出于某种原因每次用户选择日期时onChanged 都会被调用两次,这会导致多个 Navigator.pop() 的重叠导致 debugLocked 问题,我所做的只是确保 Navigator.pop() 只是使用这样的布尔变量调用一次

if(open)Navigator.pop(context, dateTime); if(open)Navigator.pop(context, dateTime); open = false;打开 = 假;

The corrected full code is as follows :更正后的完整代码如下:

import 'package:flutter/material.dart';
import 'package:flutter_date_pickers/flutter_date_pickers.dart' as dp;

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String date;
  bool open = false;


  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    date = DateTime.now().toIso8601String();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'Selected Date:',
            ),
            Text(
              '$date',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          _openDatePicker(context);
        },
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }

  void _openDatePicker(BuildContext context) {
    open = true;
    showModalBottomSheet<DateTime>(
        context: context,
        builder: (BuildContext context) {
          return Container(
            child: dp.DayPicker(
              selectedDate: DateTime.now(),
              onChanged: (dateTime) {

                if(open)Navigator.pop(context, dateTime);
                open = false;

              },
              firstDate: DateTime.now().subtract(Duration(days: 360)),
              lastDate: DateTime.now().add(Duration(days: 360)),
            ),
          );
        }).then(
      (dateTime) {
        if (dateTime != null) {
          setState(() {
            date = dateTime.toIso8601String();
          });
          print('date time: ' + date);
        } else {
          print('No Day Selected');
        }
      },
    );
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未处理的异常:&#39;package:flutter/src/widgets/navigator.dart&#39;:断言失败:第 3499 行 pos 12:&#39;!_debugLocked&#39;:不正确。”FLUTTER - Unhandled Exception: 'package:flutter/src/widgets/navigator.dart': Failed assertion: line 3499 pos 12: '!_debugLocked': is not true." FLUTTER 抛出另一个异常:'package:flutter/src/widgets/navigator.dart': Failed assertion: line 2216 pos 12: ':_debugLocked': is not true - Another exception was thrown: 'package:flutter/src/widgets/navigator.dart': Failed assertion: line 2216 pos 12: '!_debugLocked': is not true 'package:flutter/src/widgets/navigator.dart':断言失败:第 5338 行 pos 12:':_debugLocked'。 不是真的。 这是我在下面给出的代码 - 'package:flutter/src/widgets/navigator.dart': Failed assertion: line 5338 pos 12: '!_debugLocked': is not true. And this is my Code given Below 错误:_AssertionError('package:flutter/src/widgets/navigator.dart':断言失败:第 4893 行 pos 12:':_debugLocked'。不是真的。) - Error: _AssertionError ('package:flutter/src/widgets/navigator.dart': Failed assertion: line 4893 pos 12: '!_debugLocked': is not true.) 在构建过程中调用 setState() 或 markNeedsBuild()...失败的断言:第 4134 行 pos 12:“!_debugLocked”:不是真的 - setState() or markNeedsBuild() called during build…Failed assertion: line 4134 pos 12: '!_debugLocked': is not true 1-setState() 或 markNeedsBuild() 在构建期间调用。 2-断言失败:第 4165 行第 12 行:':_debugLocked':不正确 - 1-setState() or markNeedsBuild() called during build. 2-Failed assertion: line 4165 pos 12: '!_debugLocked': is not true 获取小部件库捕获的异常和失败的断言:第 3289 行 pos 12: ':_debugLocked': is not true - Getting EXCEPTION CAUGHT BY WIDGETS LIBRARY & Failed assertion: line 3289 pos 12: '!_debugLocked': is not true 坏 state:未来已经在 Flutter 完成 - Bad state: Future already completed in Flutter Flutter - 失败的断言:第 61 行 pos 12:&#39;_route == ModalRoute.of(context)&#39;:不是真的 - Flutter - Failed assertion: line 61 pos 12: '_route == ModalRoute.of(context)': is not true Flutter 中的错误:断言失败:第 588 行第 12 行:'size.isFinite': is not true.in GridTileBar - Error in Flutter: Failed assertion: line 588 pos 12: 'size.isFinite': is not true.in GridTileBar
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM