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

问题描述 投票:1回答:1

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

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

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

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

我正在使用的日历包是: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方法。不幸的是,在我的实际应用中,它变得如此缓慢,以至于无法实际使用。

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

flutter
1个回答
1
投票

如onChanged内的打印内容出于某种原因显示onChanged被调用两次每次用户选择日期时,都会导致多个Navigator.pop()的重叠导致debugLocked问题,我所做的是只需确保使用boolean这样的变量仅一次调用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');
        }
      },
    );
  }
}
热门问题
推荐问题
最新问题