Flutter 为什么Flatter NotificationListener没有捕捉到我的通知?

Flutter 为什么Flatter NotificationListener没有捕捉到我的通知?,flutter,Flutter,我对颤振中的NotificationListener有问题。我已经建立了一个简单的测试应用程序,因为我正在努力与它 单击FlatButton后,通知应该被发送,然后由onNotification中的NotificationListener捕获。 因此,预期的控制台输出将是: “测试不” “泡沫” 但我得到的只是“测试不” 因此,侦听器不会捕获通知 知道我做错了什么吗 谢谢:-) 导入“包装:颤振/材料.省道”; void main(){ runApp(MyApp()); } 类MyNotific

我对颤振中的NotificationListener有问题。我已经建立了一个简单的测试应用程序,因为我正在努力与它

单击FlatButton后,通知应该被发送,然后由onNotification中的NotificationListener捕获。 因此,预期的控制台输出将是:

“测试不”

“泡沫”

但我得到的只是“测试不”

因此,侦听器不会捕获通知

知道我做错了什么吗

谢谢:-)

导入“包装:颤振/材料.省道”;
void main(){
runApp(MyApp());
}
类MyNotification扩展了通知{
最后的字符串标题;
const MyNotification({this.title});
}
类MyApp扩展了无状态小部件{
//此小部件是应用程序的根。
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
主题:主题数据(
主样本:颜色。蓝色,
视觉密度:视觉密度。自适应平台密度,
),
主页:MyHomePage(标题:“颤振演示主页”),
);
}
}
类MyHomePage扩展StatefulWidget{
MyHomePage({Key,this.title}):超级(Key:Key);
最后的字符串标题;
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
int _计数器=0;
void _incrementCounter(){
设置状态(){
_计数器++;
});
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(widget.title),
),
正文:NotificationListener(
通知:(通知){
印刷品(“气泡”);
返回true;
},
儿童:中心(
子:列(
//水平)。
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
正文(
“您已经按了这么多次按钮:”,
),
FlatButton(onPressed:(){print(“TestNot”);MyNotification(title:“TestNot”).dispatch(context);},child:Text(“TestNot”),
正文(
“$”计数器“,
风格:Theme.of(context).textTheme.headline4,
),
],
),
)),
浮动操作按钮:浮动操作按钮(
按下时:\ u递增计数器,
工具提示:“增量”,
子:图标(Icons.add),
),//此尾随逗号使生成方法的自动格式设置更方便。
);
}
}

您无法在发送通知的同一级别接收通知。请参阅以下文件:

NotificationListener类: 侦听树上冒泡的通知的小部件

我已经更新了你的代码以使其正常工作

    import 'package:flutter/material.dart';
    
    void main() {
      runApp(MyApp());
    }
    
    class MyNotification extends Notification {
      final String title;
    
      const MyNotification({this.title});
    }
    
    class MyApp extends StatelessWidget {
      // This widget is the root of your application.
      @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: NotificationListener<MyNotification>(
              onNotification: (MyNotification notification) {
                print("Bubble");
                return true;
              },
              child: Center(
                child: Column(
                  // horizontal).
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Text(
                      'You have pushed the button this many times:',
                    ),            
                    MyChild(),
                    Text(
                      '$_counter',
                      style: Theme.of(context).textTheme.headline4,
                    ),
                  ],
                ),
              )),
          floatingActionButton: FloatingActionButton(
            onPressed: _incrementCounter,
            tooltip: 'Increment',
            child: Icon(Icons.add),
          ), // This trailing comma makes auto-formatting nicer for build methods.
        );
      }
    }
    
    
    class MyChild extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return FlatButton(
        onPressed: () {
          print("TestNot");
          MyNotification(title: "TestNot").dispatch(context);
        },
        child: Text("TestNot"));
  }
}
导入“包装:颤振/材料.省道”;
void main(){
runApp(MyApp());
}
类MyNotification扩展了通知{
最后的字符串标题;
const MyNotification({this.title});
}
类MyApp扩展了无状态小部件{
//此小部件是应用程序的根。
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
主题:主题数据(
主样本:颜色。蓝色,
视觉密度:视觉密度。自适应平台密度,
),
主页:MyHomePage(标题:“颤振演示主页”),
);
}
}
类MyHomePage扩展StatefulWidget{
MyHomePage({Key,this.title}):超级(Key:Key);
最后的字符串标题;
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
int _计数器=0;
void _incrementCounter(){
设置状态(){
_计数器++;
});
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(widget.title),
),
正文:NotificationListener(
onNotification:(我的通知){
印刷品(“气泡”);
返回true;
},
儿童:中心(
子:列(
//水平)。
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
正文(
“您已经按了这么多次按钮:”,
),            
MyChild(),
正文(
“$”计数器“,
风格:Theme.of(context).textTheme.headline4,
),
],
),
)),
浮动操作按钮:浮动操作按钮(
按下时:\ u递增计数器,
工具提示:“增量”,
子:图标(Icons.add),
),//此尾随逗号使生成方法的自动格式设置更方便。
);
}
}
类MyChild扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回按钮(
已按下:(){
打印(“TestNot”);
MyNotification(标题:“TestNot”).dispatch(上下文);
},
子项:文本(“TestNot”);
}
}

当您需要孩子通知其家长时,您可以使用

但是,当您需要反向实现通信时,换句话说,家长通知其子女,您可以使用

这里有一份关于它的好文件:


“颤振,通知‘冒泡’而价值观‘下降’”

Aha,非常感谢!我错误地将这种向上传播解释为小部件构建层次结构中的传播。。。
    import 'package:flutter/material.dart';
    
    void main() {
      runApp(MyApp());
    }
    
    class MyNotification extends Notification {
      final String title;
    
      const MyNotification({this.title});
    }
    
    class MyApp extends StatelessWidget {
      // This widget is the root of your application.
      @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: NotificationListener<MyNotification>(
              onNotification: (MyNotification notification) {
                print("Bubble");
                return true;
              },
              child: Center(
                child: Column(
                  // horizontal).
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Text(
                      'You have pushed the button this many times:',
                    ),            
                    MyChild(),
                    Text(
                      '$_counter',
                      style: Theme.of(context).textTheme.headline4,
                    ),
                  ],
                ),
              )),
          floatingActionButton: FloatingActionButton(
            onPressed: _incrementCounter,
            tooltip: 'Increment',
            child: Icon(Icons.add),
          ), // This trailing comma makes auto-formatting nicer for build methods.
        );
      }
    }
    
    
    class MyChild extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return FlatButton(
        onPressed: () {
          print("TestNot");
          MyNotification(title: "TestNot").dispatch(context);
        },
        child: Text("TestNot"));
  }
}