处置未完全处置

时间:2018-09-29 19:49:17

标签: flutter dispose

我注意到了一些奇怪的事情,很可能是因为我可能不理解这个概念。

我正在收听来自Firebase的云消息。我有2个飞镖文件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(),
    );
  }
}

因此目标很简单。通知到达时(取决于我们正在进行的活动),它应执行不同的操作。

如果在A中,则打印通知到达A

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

但是问题是,当我从B切换回A(使用A导航器从A调用B)时,它仍会打印通知到达B

要么处置不彻底,要么我遗漏了一些东西

1 个答案:

答案 0 :(得分:1)

处置没有任何幻想。处理自定义处置行为是您的工作。

更具体地说,您必须明确清理可能造成的所有混乱。在您的情况下,这意味着取消订阅firebase的流。

这表示以下内容:

StreamSubscription streamSubscription;
Stream myStream;

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

@override
void dispose() {
  super.dispose();
  streamSubscription.cancel();
}

您必须对所有侦听的流和相似的对象(例如Listenable)做相同的事情