【问题标题】:App crashes when clicking on TextFormField单击 TextFormField 时应用程序崩溃
【发布时间】:2019-09-02 18:22:33
【问题描述】:

我有一个包含产品列表的 ListView。点击 ListView 中的 ListTile 时,会显示一个 AlertDialog,其中包含一个 TextFormField,允许用户编辑产品名称。

@override
Widget build(BuildContext context) {
  return ListView.builder(
      itemCount: _products.length,
      itemBuilder: (context, index) {
        Product product = _products[index];

        return ListTile(
          title: Text(product.name),
          onTap: () {
            showDialog(
              context: context,
              builder: (BuildContext context) => AlertDialog(
                    title: Text("Edit name"),
                    content: TextFormField(
                      initialValue: product.name,
                      maxLines: 1,
                    ),
                    actions: <Widget>[
                      FlatButton(
                        child: Text("Save"),
                        onPressed: () {
                          // Save logic
                        },
                      ),
                    ],
                  ),
            );
          },
        );
      });
}

问题是当点击 TextFormField 时,应用程序立即崩溃并出现以下错误:

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following assertion was thrown building Builder(dirty):
Looking up a deactivated widget's ancestor is unsafe.
At this point the state of the widget's element tree is no longer stable. To safely refer to a
widget's ancestor in its dispose() method, save a reference to the ancestor by calling
inheritFromWidgetOfExactType() in the widget's didChangeDependencies() method.

谁能给点建议?

【问题讨论】:

  • 你有那个异常的堆栈跟踪吗?您是否覆盖了任何小部件的 dispose() 方法?
  • 你在哪里测试这个,我在 iOS 上试过,它工作正常。
  • @CopsOnRoad 我正在 iOS 模拟器上进行测试。
  • @user2181948 即使我在 iOS 模拟器 iPhone XS 中进行了测试,它也按预期工作。我应该添加对您的代码进行一些更改的屏幕录像吗?

标签: dart flutter widget


【解决方案1】:

从 AlertDialog 创建为单独的小部件,这应该可以解决问题

检查此代码:

class MyDialog extends StatefulWidget{
  final String productName;
  MyDialog({this.productName})
}

class MyDialodState extends State<MyDialog>{
  @override
  Widget build(BuildContext context) {
    reutrn AlertDialog(
                    title: Text("Edit name"),
                    content: TextFormField(
                      initialValue: widget.productName,
                      maxLines: 1,
                    ),
                    actions: <Widget>[
                      FlatButton(
                        child: Text("Save"),
                        onPressed: () {
                          // Save logic
                        },
                      ),
                    ],
                  ),
            );
  }
}

// your onTap on ListTile should look something like this:
return ListTile(
          title: Text(product.name),
          onTap: () {
            showDialog(
              context: context,
              builder: (BuildContext context) => MyDialog(
                    productName:product.name
              );
          },
        );

【讨论】:

    猜你喜欢
    • 2017-09-27
    • 2021-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多