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. Textfield は Dialog() の一部であり、「ok」と「cancel」の 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 (パッケージ:flutter/src/foundation/change_notifier.dart:112:6)

#2 ChangeNotifier.removeListener (パッケージ:flutter/src/foundation/change_notifier.dart:167:12)

#3 _AnimatedState.didUpdateWidget (パッケージ:flutter/src/widgets/transitions.dart:159:28)

#4 StatefulElement.update (パッケージ:flutter/src/widgets/framework.dart:4690:58)


試した手順:

  1. Textfield の「enable」プロパティで変数を使用して、Dispose() の前に Textfield を無効にします。
  2. Dispose() の前に TextField の enable プロパティを保持する変数が false の場合、三項演算子と if 句を使用して、TextField の「Controller」プロパティに NULL を割り当てます。
  3. TextField の enable プロパティを保持する変数が Dispose() の前に false の場合、三項演算子と if 句を使用して、TextField の 'onChanged:' プロパティに NULL を割り当てます。
4

1 に答える 1

0

エラーが発生しないようにするにFlutter/Dart: A TextEditingController was used after being disposedは、以前に破棄した TextEditingController を再度使用しないでください。ここで実行できる 1 つの方法は、AlertDialog で使用される TextEditingController の新しいインスタンスを渡すか、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 に答える