【问题标题】:How to update boolean value in Firestore from a switch tile?如何从开关磁贴更新 Firestore 中的布尔值?
【发布时间】:2022-01-13 06:19:41
【问题描述】:

我的目标是在 Firestore 中有一个 userNotifications 集合,用于跟踪用户通知首选项。用户可以通过点击开关磁贴在真假之间切换。使用下面的代码,Firestore 会根据用户交互立即正确地更新布尔值,并且用户界面正在更新并反映更改。如果用户注销并重新登录,则布尔值会准确地反映在用户界面中。

在我在代码中更广泛地应用这种方法之前,我希望有人可以发表评论,让我知道我在 Firestore 中更新布尔值的方法是否有效,或者为我指明更好的方向,以便我可以改进我的代码。 SO帖子或文档的链接很好,因为我非常愿意阅读和学习。提前感谢您的帮助。

class NotificationsMessagesTile extends StatefulWidget {

  const NotificationsMessagesTile({
    Key? key,
  }) : super(key: key);

  @override
  State<NotificationsMessagesTile> createState() =>
      _NotificationsMessagesTileState();
}

class _NotificationsMessagesTileState extends State<NotificationsMessagesTile> {
  bool notificationsActive = false;
  final String? currentSignedInUserID = Auth().currentUser?.uid;

  Future<void> updateNotifications() async {
    if (!notificationsActive) {
      notificationsActive = true;
      FirebaseFirestore.instance
          .collection('userNotifications')
          .doc(currentSignedInUserID)
          .update({
        'messages': false,
      });
    } else {
      notificationsActive = false;
      FirebaseFirestore.instance
          .collection('userNotifications')
          .doc(currentSignedInUserID)
          .update({
        'messages': true,
      });
    }
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return SwitchListTileSliver(
      icon: Provider.of<NotificationsPageProvider>(context).areMessagesTurnedOn
          ? Icons.notifications_active
          : Icons.notifications_off,
      onChanged: (bool value) {
        final provider = Provider.of<NotificationsPageProvider>(
          context,
          listen: false,
        );
        provider.updateMessagesSettings(isOn: value);
        updateNotifications();
      },
      subTitle:
      Provider.of<NotificationsPageProvider>(context).areMessagesTurnedOn
          ? const Text(
        SettingsPageString.messagesOn,
      )
          : const Text(
        SettingsPageString.messagesOff,
      ),
      title: SettingsPageString.messages,
      value:
      Provider.of<NotificationsPageProvider>(context).areMessagesTurnedOn,
    );
  }
}

【问题讨论】:

    标签: firebase flutter google-cloud-firestore flutter-layout


    【解决方案1】:

    您可以改进您的 updateNotifications() 函数,使其没有重复代码:

     Future<void> updateNotifications() async {
    
         await FirebaseFirestore.instance
              .collection('userNotifications')
              .doc(currentSignedInUserID)
              .update({
            'messages': notificationsActive,
         });
        
        setState(() {
         notificationsActive = !notificationsActive;
        });
      }
    

    我还建议您收听您的 Firestore 收藏并在更改时更新 UI。您可以查看如何做到这一点here

    【讨论】:

    • 非常感谢您改进我的代码并提供链接。我实际上还不完全明白为什么,但你的代码运行良好,所以我将查看链接并了解我的 Firestore 集合中的“监听”。
    • 欢迎您!
    猜你喜欢
    • 1970-01-01
    • 2020-06-09
    • 1970-01-01
    • 2022-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多