Flutter Shared Pref 返回多个实例而不是前景和背景之间的一个

问题描述

只要应用在前台运行,一切都可以完美运行。

这部分用于处理后台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 (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...