代码之家  ›  专栏  ›  技术社区  ›  Pushan Gupta

Dispose不完全处理

  •  1
  • Pushan Gupta  · 技术社区  · 6 年前

    我正在收听来自firebase的云信息。我有两个飞镖档案A和B。

    A看起来像:

    import 'package:flutter/material.dart';
    import 'package:firebase_messaging/firebase_messaging.dart';
    import 'package:flutter_local_notifications/flutter_local_notifications.dart';
    void main() => runApp(new MyApp());
    
    class MyApp extends StatelessWidget {
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
    
        return new MaterialApp(
          title: 'Flutter Demo',
          theme: new ThemeData(
    
            primarySwatch: Colors.blue,
          ),
          home: new MyHomePage(title: 'Flutter Demo Home Page'),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      MyHomePage({Key key, this.title}) : super(key: key);
    
      final String title;
    
      @override
      _MyHomePageState createState() => new _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      int _counter = 0;
    
      void _incrementCounter() {
        setState(() {
          _counter++;
        });
      }
      FirebaseMessaging firebaseMessaging = new FirebaseMessaging();
      FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;
      @override
      void initState() {
    
        super.initState();
        firebaseMessaging.configure(
          onLaunch: (Map<String, dynamic> msg) {
            print(" onLaunch called $msg");
          },
          onResume: (Map<String, dynamic> msg) {
            print(" onResume called ${(msg)}");
          },
          onMessage: (Map<String, dynamic> msg) {
            //showNotification(msg);
            print(" onMessage called in Activity A ${(msg)}");//--!!!!!-------!!!!->notice this
          },
        );
        firebaseMessaging.requestNotificationPermissions(
            const IosNotificationSettings(sound: true, alert: true, badge: true));
        firebaseMessaging.onIosSettingsRegistered
            .listen((IosNotificationSettings setting) {
          print('IOS Setting Registered');
        });
        firebaseMessaging.getToken().then((token) {
          print("token: "+token);
        });
        flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
        var android = new AndroidInitializationSettings('@mipmap/ic_launcher');
        var iOS = new IOSInitializationSettings();
        var initSetttings = new InitializationSettings(android, iOS);
        flutterLocalNotificationsPlugin.initialize(initSetttings);
      }
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          appBar: new AppBar(
            title: new Text(widget.title),
            actions: <Widget>[
              IconButton(
                icon: Icon(Icons.add),
                onPressed: (){
                  Navigator.push(context, MaterialPageRoute(builder: (context)=>Sample() ));// calling screen B from action of app bar
                },
              )
            ],
          ),
          body: new Container(),
        );
      }
    }
    

    请注意,如果在“活动a”中调用了新消息,我将在控制台中打印该行。。

    现在B看起来像:

    import 'package:flutter/material.dart';
    import 'package:firebase_messaging/firebase_messaging.dart';
    import 'package:flutter_local_notifications/flutter_local_notifications.dart';
    
    
    class Sample extends StatefulWidget {
    
      @override
      _SampleState createState() => _SampleState();
    }
    
    class _SampleState extends State<Sample> {
    
      @override
      void dispose(){
        super.dispose();
      }
    
    
    
    
      FirebaseMessaging firebaseMessaging1 = new FirebaseMessaging();
      FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin1;
      @override
      void initState() {
    
        super.initState();
        firebaseMessaging1.configure(
          onLaunch: (Map<String, dynamic> msg) {
            print(" onLaunch called $msg");
          },
          onResume: (Map<String, dynamic> msg) {
            print(" onResume called ${(msg)}");
          },
          onMessage: (Map<String, dynamic> msg) {
            //showNotification(msg);
            print(" onMessage called in Activity B ${(msg)}");//----!!!---!!!!---Notice this
          },
        );
        firebaseMessaging1.requestNotificationPermissions(
            const IosNotificationSettings(sound: true, alert: true, badge: true));
        firebaseMessaging1.onIosSettingsRegistered
            .listen((IosNotificationSettings setting) {
          print('IOS Setting Registered');
        });
        firebaseMessaging1.getToken().then((token) {
          print("token: "+token);
        });
        flutterLocalNotificationsPlugin1 = new FlutterLocalNotificationsPlugin();
        var android = new AndroidInitializationSettings('@mipmap/ic_launcher');
        var iOS = new IOSInitializationSettings();
        var initSetttings = new InitializationSettings(android, iOS);
        flutterLocalNotificationsPlugin1.initialize(initSetttings);
        print(firebaseMessaging1.toString());
    
      }
    
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Container(),
        );
      }
    }
    

    如果在打印通知中,则在

    如果在B,打印通知到达B

    1 回复  |  直到 6 年前
        1
  •  3
  •   Rémi Rousselet    6 年前

    处置 不做任何花哨的事。它是 处理自定义的作业 处置 行为。

    StreamSubscription streamSubscription;
    Stream myStream;
    
    @override
    void initState() {
      super.initState();
      streamSubscription = myStream.listen((foo) {
        print(foo);
      });
    }
    
    @override
    void dispose() {
      super.dispose();
      streamSubscription.cancel();
    }
    

    Listenable