3

请有人帮忙,我不确定这是否是一个框架故障,那么怎么没有更多关于这个的帖子,如果是我,那么这个错误怎么没有太多!

============================ main.dart

import 'package:flutter/material.dart';
import 'dialog_reusable.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      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> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          showDialog( context: context, builder: (context) { return MyDialog(); });
        },
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

=========================== dialog_reusable.dart

import 'package:flutter/material.dart';

import 'dialog_reusable.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      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> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          showDialog( context: context, builder: (context) { return MyDialog(); });
        },
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

============================

============================

重现步骤

  1. 文本字段是 Dialog() 的一部分,还有 2 个按钮“确定”和“取消”
  2. 使用标准选项创建一个新的 Flutter 项目
  3. 删除 LIB 文件夹中的文件
  4. 使用上面提供的代码和名称创建新文件
  5. 当 Dialog() 弹出并单击“取消”按钮时,会发生以下错误。

════════ 小部件库捕获的异常════════

在构建 MouseRegion(listeners: [enter, exit], state: _MouseRegionState#1877d) 时抛出了以下断言:处理后使用了 TextEditingController。

一旦您在 TextEditingController 上调用了 dispose(),就不能再使用它了。

相关的导致错误的小部件是:TextField file:///C:/MobileApps/Apps/Clima-Flutter/lib/utilities/mydialog.dart:90:15

抛出异常时,这是堆栈:

#0 ChangeNotifier._debugAssertNotDisposed。(包:flutter/src/foundation/change_notifier.dart:106:9)

#1 ChangeNotifier._debugAssertNotDisposed (package:flutter/src/foundation/change_notifier.dart:112:6)

#2 ChangeNotifier.removeListener (package:flutter/src/foundation/change_notifier.dart:167:12)

#3 _AnimatedState.didUpdateWidget(包:flutter/src/widgets/transitions.dart:159:28)

#4 StatefulElement.update (package:flutter/src/widgets/framework.dart:4690:58)


尝试的步骤:

  1. 通过使用 Textfield 的“启用”属性中的变量在 Dispose() 之前禁用 Textfield
  2. 如果在 Dispose() 之前保存 TextField 的启用属性的变量为 false,则通过三元运算符和 if 子句将 NULL 分配给 TextField 的“控制器”属性
  3. 如果在 Dispose() 之前持有 TextField 的启用属性的变量为 false,则通过三元运算符和 if 子句将 NULL 分配给 TextField 的“onChanged:”属性
4

1 回答 1

0

为防止出现错误Flutter/Dart: A TextEditingController was used after being disposed,不应再次使用先前处理的 TextEditingController。您可以在此处执行的一种方法是传递一个新的 TextEditingController 实例以在 AlertDialog 中使用,或者取决于您如何使用 TextEditingController。

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

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

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  var textEditingController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Text(
            'You have entered ${textEditingController.text}',
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () => alertDialog(textEditingController),
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }

  alertDialog(TextEditingController textEditingController) {
    return showDialog<String>(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          content: TextField(
            controller: textEditingController,
            decoration: const InputDecoration(hintText: 'Enter Something'),
          ),
          actions: <Widget>[
            TextButton(
              child: const Text(
                "Cancel",
                style: TextStyle(color: Colors.black),
              ),
              onPressed: () => Navigator.of(context).pop(),
            ),
            TextButton(
              child: const Text(
                "OK",
                style: TextStyle(color: Colors.red),
              ),
              onPressed: () {
                setState(() {
                  // Triggers a Widget rebuild to update textEditingController state
                });
                Navigator.of(context).pop();
              },
            ),
          ],
        );
      },
    );
  }
}

于 2021-09-20T15:57:57.573 回答