cmdrootaccess / another-flushbar

A flexible widget for user notification. Customize your text, button, duration, animations and much more. For Android devs, it is made to replace Snackbars and Toasts.
https://pub.dev/packages/another_flushbar
MIT License
145 stars 88 forks source link

Unable to create a Dismiss button as the `mainButton` #123

Closed thefirebanks closed 12 months ago

thefirebanks commented 12 months ago

Describe the bug Trying to create a Flushbar that gets dismissed when the user presses a button following the example here, but 2 issues come up:

To Reproduce Steps to reproduce the behavior:

This is my current code:

class NotificationService {
  final FirebaseMessaging _firebaseMessaging = FirebaseMessaging.instance;

  void initialize(BuildContext context) async {
    await _firebaseMessaging.requestPermission();
    String? initialToken = await _firebaseMessaging.getToken();

    if (initialToken != null) {
      // Send the initial token to your server for user/device association
      print("Initial Device Token: $initialToken");
    }

    _firebaseMessaging.onTokenRefresh.listen((newToken) {
      // Send the new token to your server for user/device association
      print("Refreshed Device Token: $newToken");
    });

    FirebaseMessaging.onMessage.listen(
      (RemoteMessage message) {
        // Handle the incoming message here
        print(
            'Foreground notification received: ${message.notification?.title}');

        String notificationTitle = message.notification?.title ?? '';
        String notificationMessage = message.notification?.body ?? '';

        Flushbar flush;
        flush = Flushbar<bool>(
          title: notificationTitle,
          message: notificationMessage,
          flushbarPosition: FlushbarPosition.TOP,
          flushbarStyle: FlushbarStyle.FLOATING,
          duration: Duration(seconds: 5),
          mainButton: TextButton(
            child: Text(
              "Dismiss",
              style: TextStyle(color: Colors.amber),
            ),
            onPressed: () {
              flush.dismiss(true);
            },
          ),
        );
        flush.show(context);

      },
    );

    FirebaseMessaging.onMessageOpenedApp.listen(
      (RemoteMessage message) {
        // Handle when the user taps the notification while the app is in the background
        print("User tapped the notification: ${message.notification?.title}");
      },
    );
  }

}

Which doesn't fully build because of the error, so I can't initialize the app.

Expected behavior The app should initialize and the button should be created.

Smartphone (please complete the following information):

thefirebanks commented 12 months ago

UPDATE: Solved it by adding a late modifier to flush:

        late Flushbar flush;
        flush = Flushbar<bool>(
          title: notificationTitle,
          message: notificationMessage,
          flushbarPosition: FlushbarPosition.TOP,
          flushbarStyle: FlushbarStyle.FLOATING,
          duration: Duration(seconds: 5),
          mainButton: TextButton(
            child: Text(
              "Dismiss",
              style: TextStyle(color: Colors.amber),
            ),
            onPressed: () {
              flush.dismiss(true);
            },
          ),
        );
        flush.show(context);

But I'll keep this here in case anyone else runs into a similar issue

rt234cw commented 10 months ago

finally found the solution!!! Big thanks