Flutter 颤振全局函数更新有状态小部件状态
我正在编写一个颤振应用程序,它使用一个全局函数来处理Pushy.me通知。此函数需要更新有状态小部件的状态 我尝试了一个全局键来访问widgets的当前状态,但它什么也没做。我尝试了一个Eventify发射器,发射器和侦听器似乎没有对齐Flutter 颤振全局函数更新有状态小部件状态,flutter,pushy,Flutter,Pushy,我正在编写一个颤振应用程序,它使用一个全局函数来处理Pushy.me通知。此函数需要更新有状态小部件的状态 我尝试了一个全局键来访问widgets的当前状态,但它什么也没做。我尝试了一个Eventify发射器,发射器和侦听器似乎没有对齐 import 'package:eventify/eventify.dart'; EventEmitter emitter = new EventEmitter(); GlobalKey<_WrapperScreenState> _key = Gl
import 'package:eventify/eventify.dart';
EventEmitter emitter = new EventEmitter();
GlobalKey<_WrapperScreenState> _key = GlobalKey<_WrapperScreenState>();
void backgroundNotificationListener(Map<String, dynamic> data) {
// Print notification payload data
print('Received notification: $data');
// Notification title
String notificationTitle = 'MyApp';
// Attempt to extract the "message" property from the payload: {"message":"Hello World!"}
String notificationText = data['message'] ?? 'Hello World!';
Pushy.notify(notificationTitle, notificationText, data);
emitter.emit('updateList',null,"");
try{
print(_key.currentState.test);
}
catch(e){
print(e);
}
// Clear iOS app badge number
Pushy.clearBadge();
}
class WrapperScreen extends StatefulWidget {
@override
_WrapperScreenState createState() => _WrapperScreenState();
}
一个不寻常的替代方法是创建一个单例类来保存所需的数据类型,以便在小部件使用它的同时更新单例。另一个推荐的替代方案是使用全局状态管理构建体系结构。@JustCase我今天尝试切换到使用带有ChangeNotifiers的提供程序。这将给我更多的全球状态,可以在更多的地方更新。现在的问题是全局函数似乎没有访问上下文的权限,如果我将变量作为全局变量创建给观察者,则在函数内部调用时仍然为null。似乎函数是在设置变量之前声明的,它从未看到变量的更新值。。。有什么想法吗?