gpt4 book ai didi

flutter - 处理不完全处理

转载 作者:IT王子 更新时间:2023-10-29 07:00:06 25 4
gpt4 key购买 nike

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

我正在收听来自 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(),
);
}
}

如果在“Activity 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(B 是使用 Navigator push 从 A 调用的)时,它仍然打印 Notification arrived in B

要么处置没有完全处置,要么我遗漏了什么

最佳答案

dispose 不会做任何花哨的事情。处理自定义处理行为是的工作。

更具体地说,您必须明确清理您可能造成的所有困惑。在您的情况下,这意味着取消订阅 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com