flutter坏状态:将来已经完成,并且断言失败:2113行pos 12:'!_debugLocked':不正确

时间:2020-03-05 02:19:14

标签: flutter

此站点上的第一篇文章,因此,如果有任何格式化问题的提示或不遵循准则,请告诉我。谢谢

在flutter应用程序中,我正在使用嵌套在modalBottomSheet中的第三方日历小部件进行创建。在onChanged方法中使用Navigator.pop关闭日历小部件时,将引发以下错误:

'package:flutter / src / widgets / navigator.dart':断言失败:2330行pos 12:'!_debugLocked':不正确。

如果我在创建日历的同一个月中选择一天,则不会出现错误。仅当我后退或前进一个月并选择一个日期时,才会出现该错误。当我向前(或向后)移动一个月然后回到创建日历的月份时,也会出现错误。例如,如果我使用今天的日期作为所选日期,则该月份为3月。如果我使用箭头返回到2月或前进到4月并选择一个日期,则会收到上述错误。

我正在使用的日历包是:flutter_date_pickers:^ 0.0.5

我从下面的代码创建了一个最小运行示例:

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');
        }
      },
    );
  }
}

我仔细研究了其他具有类似错误的问题,并尝试了所有不同的方法以一种对我有用的方法来解决此问题。它将Navigator.pop转换为Navigator.pushNamedAndReplace方法。不幸的是,在我的实际应用中,它变得如此缓慢,以至于无法实际使用。

任何帮助将不胜感激。谢谢

1 个答案:

答案 0 :(得分:1)

由于onChanged内的打印内容由于某种原因而显示,每次用户选择日期时,都会两次 onChanged被调用,这会导致多个Navigator.pop()重叠,从而导致debugLocked问题 strong>,我所做的只是确保使用这样的布尔变量

一次调用Navigator.pop()。

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

更正后的完整代码如下:

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');
        }
      },
    );
  }
}