简体   繁体   中英

Flutter - widget variables are always null

I have a Flutter app connected to firebase, and I'm using it to receive push notifications from Firebase Cloud Messaging, using this code. Whenever I call the variables widget.title , widget.body , they are null, but when the method receives a notification, they have the value that came from FCM. What should I do?

class PushList extends StatefulWidget {
  
  Map<dynamic, dynamic> notific;
  String title, body;

  Future<dynamic> fcmMessageReceiver() async {
    FirebaseMessaging.instance.getInitialMessage().then((value) {
      if (value != null) {}
    });

    FirebaseMessaging.onMessage.listen((RemoteMessage message) {
      if (message.notification != null) {
        notificacao = {
          'title': message.notification.title,
          'body': message.notification.body
        };
        title = message.notification.title;
        body = message.notification.body;

        print('MENSAGEM: $notific');
      }
    });
    FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {});
  }

  PushList() {
  }

  @override
  _PushListState createState() => _PushListState();
}

class _PushListState extends State<PushList> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      extendBody: true,
      backgroundColor: Colors.white,
      appBar: null,
      body: ListView(
        children: [
          widget.notific != null
              ? Card(
                  margin: EdgeInsets.all(10),
                  elevation: 4,
                  child: ListTile(
                    title: Text(
                      widget.title,
                    ),
                    subtitle: Text(
                      widget.body,
                    ),
                  ),
                )
              : Container(
                  child: Text("U don't have new notifcs."),
                ),
        ],
      ),
    );
  }
}

PushList should be immutable. Define the properties inside the state and call fcmMessageReceiver inside initState . Also you need to call setState to trigger rebuild after you set title and body :

class PushList extends StatefulWidget {
  @override
  _PushListState createState() => _PushListState();
}

class _PushListState extends State<PushList> {
  Map<dynamic, dynamic> notific;
  String title, body;

  @override
  void initState() {
    super.initState();
    fcmMessageReceiver();
  }

  Future<dynamic> fcmMessageReceiver() async {
    FirebaseMessaging.instance.getInitialMessage().then((value) {
      if (value != null) {}
    });

    FirebaseMessaging.onMessage.listen((RemoteMessage message) {
      if (message.notification != null) {
        if (mounted) {
          setState(() {
            notificacao = {
              'title': message.notification.title,
              'body': message.notification.body
            };
            title = message.notification.title;
            body = message.notification.body;
          });
        }
        print('MENSAGEM: $notific');
      }
    });
    FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      extendBody: true,
      backgroundColor: Colors.white,
      appBar: null,
      body: ListView(
        children: [
          notific != null
              ? Card(
                  margin: EdgeInsets.all(10),
                  elevation: 4,
                  child: ListTile(
                    title: Text(
                      title ?? '',
                    ),
                    subtitle: Text(
                      body ?? '',
                    ),
                  ),
                )
              : Container(
                  child: Text("U don't have new notifcs."),
                ),
        ],
      ),
    );
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM