El Hombre Sin Nombre
El Hombre Sin Nombre

Reputation: 3102

Flutter - Get Array in Firebase Messaging

In Flutter i try to get array in new firebase_messaging 10.0.0 like this

import 'dart:io';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:pushtest/functions/alert_messages.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:pushtest/constant/constant.dart' as Constants;

class PushNotifications {
  FirebaseMessaging firebaseMessaging;

  initNotifications() {
    firebaseMessaging.requestPermission();
    firebaseMessaging.getToken().then((token) async {
      SharedPreferences prefs = await SharedPreferences.getInstance();
      prefs.setString('firebaseToken', token);
    });
  }

  configuration(scaffold, location) {
    FirebaseMessaging.onMessage.listen((RemoteMessage message) {
      var noti;
      message as Map;
      if (Platform.isIOS) {
        noti = message['message'];
      } else {
        noti = message['data']['message'];
      }
      if (location == 'navigation') {
        Messages.alert(scaffold.currentContext, noti);
      } else {
        Constants.message = noti;
      }
      return null;
    });

    FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
      var noti;
      if (Platform.isIOS) {
        noti = message['message'];
      } else {
        noti = message['data']['message'];
      }
      Constants.message = noti;
      return null;
    });
  }
}

Problem: When i try to do

 noti = message['message'];

Return

The operator '[]' isn't defined for the type 'RemoteMessage'. Try defining the operator '[]'.

So what is the proper way to get array elements with listen functions?

Upvotes: 0

Views: 158

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80934

If you are sending data messages then do:

noti = message.data['message'];

Upvotes: 2

Related Questions