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

ben*_*ter 5 sharedpreferences dart flutter

我已经Firebase Cloud Messaging在我的扑动应用程序中实现了。一切正常,但我想将所有消息列表存储在本地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");
  }
}
Run Code Online (Sandbox Code Playgroud)

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

Oma*_*att 0

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