问题描述
只要应用在前台运行,一切都可以完美运行。
这部分用于处理后台firebase消息并准备通知对象并保存收到的通知。这部分将json编码的通知消息写入共享pref
在后台或应用终止
Future<dynamic> onBackgroundMessage(Map<String,dynamic> message) async
{
//Custom Notification Object
NotificationMessage test=NotificationMessage().fromMap(message);
//Shared Pref Custom
PsSharedPreferences.instance.futureShared.then((value)
{
value.setString(Const.VALUE_HOLDER_NOTIFICATION_MESSAGE,json.encode(test.toJson()));
PsSharedPreferences.instance.replaceNotificationMessage(json.encode(test.toJson()));
print("Notification Message "+PsSharedPreferences.instance.getNotificationMessage());
print("Notification Message "+value.getString(Const.VALUE_HOLDER_NOTIFICATION_MESSAGE));
});
//Core Shared Pref
Future<SharedPreferences> futureShared = SharedPreferences.getInstance();
futureShared.then((SharedPreferences shared)
{
NotificationMessage notificationMessage=NotificationMessage().fromMap(message);
shared.setString(Const.VALUE_HOLDER_NOTIFICATION_MESSAGE,json.encode(notificationMessage.toJson()));
print("Notification Message Shared ${json.encode(shared.getString(Const.VALUE_HOLDER_NOTIFICATION_MESSAGE))}");
});
}
日志
Notification Message Shared //Some json encoded object
Notification Message //Some json encoded object
Notification Message //Some json encoded object
在前台
@override
void initState()
{
print("Notification Message "+PsSharedPreferences.instance.getNotificationMessage());
print("Notification Message "+PsSharedPreferences.instance.shared.getString(
Const.VALUE_HOLDER_NOTIFICATION_MESSAGE)
);
PsSharedPreferences.instance.futureShared.then((value)
{
//Not Working with reload as well
value.reload();
print("Notification Message "+value.getString(Const.VALUE_HOLDER_NOTIFICATION_MESSAGE));
print("Notification Message "+PsSharedPreferences.instance.getNotificationMessage());
});
Future<SharedPreferences> futureShared = SharedPreferences.getInstance();
futureShared.then((SharedPreferences shared)
{
print("Notification Message Shared
${json.encode(shared.getString(Const.VALUE_HOLDER_NOTIFICATION_MESSAGE))}");
notificationMessage=NotificationMessage.fromJson(json.decode(
shared.getString(Const.VALUE_HOLDER_NOTIFICATION_MESSAGE))
);
});
super.initState();
}
日志
Notification Message //nothing
Notification Message //nothing
Notification Message //nothing
Notification Message //nothing
Notification Message Shared //nothing
[ERROR:Flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: FormatException: Unexpected end of input (at character 1)
自定义共享偏好
class PsSharedPreferences
{
PsSharedPreferences()
{
Utils.psPrint('init PsSharePerference $hashCode');
futureShared = SharedPreferences.getInstance();
futureShared.then((SharedPreferences shared)
{
this.shared = shared;
});
}
Future<SharedPreferences> futureShared;
SharedPreferences shared;
// Singleton instance
static final PsSharedPreferences _singleton = PsSharedPreferences();
// Singleton accessor
static PsSharedPreferences get instance => _singleton;
Future<dynamic> replaceNotificationMessage(String message) async
{
await shared.setString(Const.VALUE_HOLDER_NOTIFICATION_MESSAGE,message);
}
String getNotificationMessage()
{
return shared.getString(Const.VALUE_HOLDER_NOTIFICATION_MESSAGE);
}
}
现在的问题是我得到了 SharedPref 的不同实例,这就是为什么我得到空结果,或者我做错了什么。在前台,一切都很完美。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)