flutter - 使用共享首选项列表抖动保存和检索通知

标签 flutter dart sharedpreferences

我已实现 Firebase Cloud Messaging在我的 flutter 应用程序中。一切正常,但我想使用 shared preferences 在本地存储所有消息列表并在另一个屏幕中检索它们。我所有的逻辑都无法正常工作,因为当 onMessage 出现时我无法保存消息函数被调用。

推送通知服务

class PushNotificationService {
  final FirebaseMessaging _fcm = FirebaseMessaging();
  List<String> titles;
  List<String> msgs;
  Future initialise() async {
    notiList = List<NotiMessage>();
    if (Platform.isIOS) {
      // request permissions if we're on android
      _fcm.requestNotificationPermissions(IosNotificationSettings());
      _fcm.configure();
      // For testing purposes print the Firebase Messaging token
      String token = await _fcm.getToken();
      print("FirebaseMessaging token: $token");
    } else{
      String token = await _fcm.getToken();
      print("FirebaseMessaging token: $token");
    }

    _fcm.configure(
      // Called when the app is in the foreground and we receive a push notification
      onMessage: (Map<String, dynamic> message) async {
        print('onMessage: $message');
//add list of messages to shared preferences
        _setMessage(message);
      },
      // Called when the app has been closed comlpetely and it's opened
      // from the push notification.
      onLaunch: (Map<String, dynamic> message) async {
        print('onLaunch: $message');
        _serialiseAndNavigate(message);
//add list of messages to shared preferences
        _setMessage(message);
      },
      // Called when the app is in the background and it's opened
      // from the push notification.
      onResume: (Map<String, dynamic> message) async {
        print('onResume: $message');
        _serialiseAndNavigate(message);
//add list of messages to shared preferences
        _setMessage(message);
      },
    );
  }

  void _serialiseAndNavigate(Map<String, dynamic> message) {
    var notificationData = message['data'];
    var view = notificationData['view'];

    if (view != null) {
      // Navigate to desired page
      if (view == 'create_post') {

      }
    }
  }
  _setMessage(Map<String, dynamic> message) {
//add list of messages to shared preferences
    final notification = message['notification'];
    final data = message['data'];
    final String title = notification['title'];
    final String body = notification['body'];
    String mMessage = data['message'];
    //add to list
    titles.add(title);
    msgs.add(mMessage);
    //save to shared preferences (does not work)
    storeTitles(titles);
    storeMsgs(msgs);
    print("Title: $title, body: $body, message: $mMessage");

  }
  void storeTitles(List<String> list) async{
    SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.setStringList("notiTitles", list);
//list returns null
  }
  void storeMsgs(List<String> list) async{
    SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.setStringList("notiMsgs", list);
  }
  Future<List<String>> getTitles(List<String> list) async{
    SharedPreferences prefs = await SharedPreferences.getInstance();
    list = prefs.getStringList("notiTitles");
 return prefs.getStringList("notiTitles");
  }
  Future<List<String>> getMsgs(List<String> list) async{
    SharedPreferences prefs = await SharedPreferences.getInstance();
    list = prefs.getStringList("notiMsgs");
  return prefs.getStringList("notiMsgs");
  }
}

实现这一目标的最佳方法是什么。我想持久保存消息并在另一个屏幕中调用它们。请帮我。

最佳答案

保存列表的代码 shared_preferences似乎没问题。问题可能在于如何获取数据。如果您要存储关键数据,我建议最好使用类似 provider 的东西。反而。 shared_preferences插件无法保证在返回后将写入持久化到磁盘,如其 docs 中所述。 .

关于flutter - 使用共享首选项列表抖动保存和检索通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60886098/

相关文章:

android - 建立失败的 flutter

firebase - 将Firestore数据过滤为仅包含当前用户信息

java - 以编程方式更改首选项值后更新它们

flutter - 我如何从Dart流中的回调函数产生值

api - flutter : unhandled exception: invalid argument

dart - 尝试根据 JSON 字符串值动态设置图标

java - 在 View 类中保存 sharedPreferences

android - 无法使用 PreferenceActivity 设置首选项

android - Flutter .apk 已构建但未安装在设备/模拟器中

flutter - Flutter:如何仅使用其中一个子项覆盖整个专栏