Flutter:"Looking up a deactivated widget's ancestor is unsafe" 在无状态 Widget 中

Flutter: "Looking up a deactivated widget's ancestor is unsafe" in a Stateless Widget

我是Flutter新手,这个问题很困扰我,我上网搜索了一下,结果none让我满意:

我尝试使用包中的进度对话框:

import 'package:progress_dialog/progress_dialog.dart';

而我的 main.dart 文件中的 class MyApp 是这样的:

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // Declare and decorate my Progress Dialog.
    ProgressDialog pr = ProgressDialog(
      context,
      type: ProgressDialogType.Normal,
      isDismissible: false,
    );
    pr.style(
      message: 'Fetching Something...',
      borderRadius: 50.0,
      elevation: 5.0,
    );

    // TODO: implement build method
    return MaterialApp(
      home: Scaffold(
        body: RaisedButton.icon(
          onPressed: () async {
            pr.show();
            await fetchData();
            pr.hide();
          },
          icon: Icon(Icons.clear),
          label: Text('Fetch Data'),
        ),
      ),
    );
  }
}

而我的示例fetchData()函数是这样的(当然Firestore的函数的打包和安装步骤都是经过验证的):

Future<void> fetchData() async {
  // Just an example of really fetching something.
  await Firestore.instance
      .collection('users')
      .document('0')
      .delete();
}

我想要的是,每次单击按钮时,加载微调器都会显示并在 fetchData() 功能完成后立即隐藏。这会在第一次单击时产生正确的流程,但是,如果我第二次单击该按钮,则不会显示微调器(fetchData() 函数仍会正确执行)。并在终端中显示警告(不是错误):

I/flutter (17942): Exception while showing the dialog
I/flutter (17942): Looking up a deactivated widget's ancestor is unsafe.
I/flutter (17942): At this point the state of the widget's element tree is no longer stable.
I/flutter (17942): To safely refer to a widget's ancestor in its dispose() method, save a reference to the ancestor by calling dependOnInheritedWidgetOfExactType() in the widget's didChangeDependencies() method.

dependOnInheritedWidgetOfExactType() 上的文档非常有限且难以理解。所以我仍然不知道如何正确解决这个问题。

非常感谢任何帮助。谢谢。

您可以复制粘贴 运行 下面的完整代码
您可以使用 await pr.show();await pr.hide();
代码片段

onPressed: () async {
        await pr.show();
        await fetchData();
        await pr.hide();
      },

工作演示

完整代码

import 'package:flutter/material.dart';
import 'package:progress_dialog/progress_dialog.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++;
    });
  }

  Future<void> fetchData() async {
    await Future.delayed(Duration(seconds: 3), () {});
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    ProgressDialog pr = ProgressDialog(
      context,
      type: ProgressDialogType.Normal,
      isDismissible: false,
    );
    pr.style(
      message: 'Fetching Something...',
      borderRadius: 50.0,
      elevation: 5.0,
    );

    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            RaisedButton.icon(
              onPressed: () async {
                await pr.show();
                await fetchData();
                await pr.hide();
              },
              icon: Icon(Icons.clear),
              label: Text('Fetch Data'),
            ),
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}