gpt4 book ai didi

flutter/Dart : A TextEditingController was used after being disposed

转载 作者:行者123 更新时间:2023-12-05 08:05:56 26 4
gpt4 key购买 nike

拜托,有人帮忙解决这个问题,我不确定这是否是一个框架故障,那么怎么没有更多关于这个的帖子,如果是我那么为什么这个错误没有太多!

===========================主.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. Textfield 是 Dialog() 的一部分,还有 2 个按钮“确定”和“取消”
  2. 使用标准选项创建一个新的 Flutter 项目
  3. 删除LIB文件夹中的文件
  4. 使用上面提供的代码和名称制作新文件
  5. 当 Dialog() 弹出并单击“取消”按钮时,会发生以下错误。

════════ 组件库捕获异常════════

构建 MouseRegion(监听器:[进入,退出],状态:_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(包: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. 在 Dispose() 之前使用文本字段的“启用”属性中的变量禁用文本字段
  2. 如果在 Dispose() 之前保存 TextField 启用属性的变量为 false,则通过三元运算符和 if 子句将 NULL 分配给 TextField 的“Controller”属性
  3. 如果在 Dispose() 之前保存 TextField 启用属性的变量为 false,则通过三元运算符和 if 子句将 NULL 分配给 TextField 的“onChanged:”属性

最佳答案

为防止出现错误 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();
},
),
],
);
},
);
}
}

关于 flutter/Dart : A TextEditingController was used after being disposed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62840176/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com