【问题标题】:Why is Navigator.pop failing to pass a parameter back?为什么 Navigator.pop 无法传回参数?
【发布时间】:2020-07-29 01:51:30
【问题描述】:

我正在启动一个警报对话框以确认用户保存或清除过滤器的操作,并且我想根据他们的选择返回一个布尔值。我试图通过 Navigator.pop() 传递它,但不断收到此错误:

The following _TypeError was thrown while handling a gesture:
type 'bool' is not a subtype of type 'AlertDialog' of 'result' 

有人知道为什么会这样吗?这是我的代码。具体错误发生在我将 showDialog 的结果分配给 var shouldClear 的 onPressed 中。

import 'package:flutter/material.dart';

class FilterNavbar extends StatelessWidget {

  final VoidCallback clearFilter;

  const FilterNavbar({@required this.clearFilter});

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 60,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          Container(
            width: MediaQuery.of(context).size.width * .3,
            child: RaisedButton(
              onPressed: () async {
                var shouldClear = await showDialog<AlertDialog>(
                  context: context,
                  builder: (context) { 
                    return generateDialog(context, attemptSave: false);
                  }
                );

              },
              child: const Text("Clear"),
            ),
          ),
          Container(
            width: MediaQuery.of(context).size.width * .3,
            child: RaisedButton(
              onPressed: () async {
                await showDialog<AlertDialog>(
                  context: context,
                  builder: (context) { 
                    return generateDialog(context, attemptSave: true);
                  }
                );
                Navigator.pop(context, true);
              },
              child: const Text("Save"),            
            ),
          )
        ]
      ),
    );
  }
}

AlertDialog generateDialog(BuildContext context, {bool attemptSave}){

  return AlertDialog(
    title: Center(child: Text("${attemptSave ? "Save": "Clear"} filter?")),
    actions: [
      FlatButton(
        onPressed: () {
          if (attemptSave) {
            Navigator.pop(context, false);
          }
          else {
            Navigator.pop(context, true);
          }
        }, 
        child: Text("${attemptSave ? "Save": "Clear"}")
      )
    ],
  );
}

【问题讨论】:

    标签: flutter dart navigation dialog


    【解决方案1】:

    您可以在下面复制粘贴运行完整代码
    请将AlertDialog 更改为bool
    来自

    await showDialog<AlertDialog>
    

    await showDialog<bool>
    

    工作演示

    完整代码

    import 'package:flutter/material.dart';
    
    class FilterNavbar extends StatelessWidget {
      final VoidCallback clearFilter;
    
      const FilterNavbar({@required this.clearFilter});
    
      @override
      Widget build(BuildContext context) {
        return Container(
          height: 60,
          child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                Container(
                  width: MediaQuery.of(context).size.width * .3,
                  child: RaisedButton(
                    onPressed: () async {
                      var shouldClear = await showDialog<bool>(
                          context: context,
                          builder: (context) {
                            return generateDialog(context, attemptSave: false);
                          });
                    },
                    child: const Text("Clear"),
                  ),
                ),
                Container(
                  width: MediaQuery.of(context).size.width * .3,
                  child: RaisedButton(
                    onPressed: () async {
                      await showDialog<bool>(
                          context: context,
                          builder: (context) {
                            return generateDialog(context, attemptSave: true);
                          });
                      Navigator.pop(context, true);
                    },
                    child: const Text("Save"),
                  ),
                )
              ]),
        );
      }
    }
    
    AlertDialog generateDialog(BuildContext context, {bool attemptSave}) {
      return AlertDialog(
        title: Center(child: Text("${attemptSave ? "Save" : "Clear"} filter?")),
        actions: [
          FlatButton(
              onPressed: () {
                if (attemptSave) {
                  Navigator.pop(context, false);
                } else {
                  Navigator.pop(context, true);
                }
              },
              child: Text("${attemptSave ? "Save" : "Clear"}"))
        ],
      );
    }
    
    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>[
                Expanded(
                    child: FilterNavbar(
                  clearFilter: () {},
                )),
                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),
          ),
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-19
      • 2011-08-17
      • 2015-01-24
      • 1970-01-01
      相关资源
      最近更新 更多