【问题标题】:Flutter use variable from another classFlutter 使用来自另一个类的变量
【发布时间】:2022-12-18 10:55:13
【问题描述】:

我如何在 flutter 中使用另一个类中的变量?我想将变量 (randomName) 传递给另一个类并使用它。我该怎么做?

Future<void> showNotification(int id, String title, String body) async {
    // Generate a new random name
    final random = new Random();
    final randomName = names[random.nextInt(names.length)];

    await flutterLocalNotificationsPlugin.periodicallyShow(
      id,
      title,
      body = randomName,
      RepeatInterval
          .everyMinute, //schedule the notification to show after 2 seconds.
      const NotificationDetails(
        // Android details
        android: AndroidNotificationDetails('main_channel', 'Main Channel',
            channelDescription: "hello",
            importance: Importance.max,
            priority: Priority.max),
        // iOS details
        iOS: DarwinNotificationDetails(
          sound: 'default.wav',
          presentAlert: true,
          presentBadge: true,
          presentSound: true,
        ),
      ),

      // Type of time interpretation
      androidAllowWhileIdle:
          true, // To show notification even when the app is closed
    );
  }
 onPressed: () {
                      setState(() {
                        showToast();
                        NotificationService().showNotification(
                            1, 'Hello ${widget.userPost}', 'You');
                      });
                    },

我想在这里用 in 代替“你”。或者我可以直接从 showNotification 使用?提前致谢

我试过@override

【问题讨论】:

    标签: flutter


    【解决方案1】:

    要在 Flutter 中将变量从一个类传递到另一个类,您可以在创建该类的实例时使用构造函数将变量传递给另一个类。

    在您的示例中,当您在 onPressed 回调中创建该类的实例时,您可以将 randomName 变量传递给 NotificationService 类。然后,您可以使用 this 关键字访问 NotificationService 类中的 randomName 变量。

    这是您如何执行此操作的示例:

    class NotificationService {
      String randomName;
    
      NotificationService(this.randomName);
    
      Future<void> showNotification(int id, String title, String body) async {
        await flutterLocalNotificationsPlugin.periodicallyShow(
          id,
          title,
          body = randomName,
          RepeatInterval.everyMinute, //schedule the notification to show after 2 seconds.
          const NotificationDetails(
            // Android details
            android: AndroidNotificationDetails('main_channel', 'Main Channel',
                channelDescription: "hello",
                importance: Importance.max,
                priority: Priority.max),
            // iOS details
            iOS: DarwinNotificationDetails(
              sound: 'default.wav',
              presentAlert: true,
              presentBadge: true,
              presentSound: true,
            ),
          ),
    
          // Type of time interpretation
          androidAllowWhileIdle:
              true, // To show notification even when the app is closed
        );
      }
    }
    
    onPressed: () {
      setState(() {
        showToast();
        final random = new Random();
        final randomName = names[random.nextInt(names.length)];
        NotificationService(randomName).showNotification(
            1, 'Hello ${widget.userPost}', 'You');
      });
    },
    

    或者,您可以使 randomName 变量成为两个类都可以访问的全局变量,但通常不建议这样做,因为它会导致代码难以维护。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-27
      • 1970-01-01
      • 1970-01-01
      • 2020-03-12
      相关资源
      最近更新 更多