我注意到一些奇怪的事情,很可能是因为我不理解这个概念。
我正在收听来自FireBase的云消息。我有两个DART文件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
但问题是,当我从B切换回A时(B是从使用导航器的push调用的),它仍然打印到达B的通知。
要么处置不彻底,要么我丢失了什么东西

最佳答案

处置不做任何花哨的事。处理自定义释放行为是您的工作。
更具体地说,你必须明确地清理你可能造成的所有混乱。在您的情况下,这意味着取消订阅FireBase的流。
这转化为以下内容:

StreamSubscription streamSubscription;
Stream myStream;

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

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

您必须对所有收听的流和类似对象(如Listenable)执行相同的操作。

关于flutter - 配置不完全配置,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52571976/

10-11 04:38