简体   繁体   English

如何在 Flutter 中创建新用户后将默认用户名、主题模式和主题颜色存储到 Cloud Firestore?

[英]How to store default userName, themeMode and themeColor to Cloud Firestore after creating new user in Flutter?

When a new user is created in Firebase, I want to create a collection of user preferences and save it to Cloud Firestore as well.当在 Firebase 中创建新用户时,我想创建一个用户首选项集合并将其保存到 Cloud Firestore。 All defaulted to username String 'John Doe', thememode ThemeMode.light and themecolor FlexScheme.money (using flexcolorscheme package).全部默认为用户名字符串 'John Doe',主题模式 ThemeMode.light 和 themecolor FlexScheme.money(使用 flexcolorscheme 包)。

I can store Strings and int's (works fine with my code), but not ThemeModes apparently and I don't know how to solve this correctly.我可以存储字符串和整数(适用于我的代码),但显然不能存储 ThemeModes,我不知道如何正确解决这个问题。

I created a PrefService class:我创建了一个 PrefService class:

class PrefService {
  //Declare a nullable String to hold user unique ID
  final String? uid;

  //When we call PrefService, require to insert user unique ID
  PrefService({this.uid});

  final CollectionReference<Object?> userPreferences =
      FirebaseFirestore.instance.collection('userPreferences');

  Future<dynamic> updateUserPreferences(
    String name,
    ThemeMode themeMode,
    FlexScheme themeColor,
  ) async {
    //If document doesn't exist yet in Firebase, it will be created under user unique ID
    return userPreferences.doc(uid).set(<String, dynamic>{
      'name': name,
      'themeMode': themeMode,
      'themeColor': themeColor,
    });
  }
}

And an AuthService class:还有一个 AuthService class:

class AuthService {
  //Sign UP user only with email and password
  Future<void> signUp({
    required String email,
    required String password,
    required BuildContext context,
  }) async {
    await FirebaseAuth.instance.createUserWithEmailAndPassword(
      email: email,
      password: password,
    );
    //After creating a user, create a database file under user unique ID as well
    //Store default name, themeMode and themeColor, which can be adjusted later by user.
    Logger().i('Database record created for user');
    await PrefService(uid: FirebaseAuth.instance.currentUser!.uid)
        .updateUserPreferences(
      'John Doe',
      ThemeMode.light,
      FlexScheme.money,
    );
etc...

When I set all parameters of updateUserPreferences() to type String, everything works and I get a nice collection under a unique user ID.当我将 updateUserPreferences() 的所有参数设置为 String 类型时,一切正常,并且我在唯一用户 ID 下得到了一个很好的集合。 But it does not let me store parameter of type ThemeMode:但它不允许我存储 ThemeMode 类型的参数:

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Invalid argument: Instance of 'ThemeMode'

Why can I store a String, but not a ThemeMode?为什么我可以存储一个字符串,但不能存储一个 ThemeMode? I want users to be able to change these settings in a SettingsScreen(), but how should I store them correctly?我希望用户能够在 SettingsScreen() 中更改这些设置,但我应该如何正确存储它们?

I am aware of the existence of SharedPreferences, but I want to do it this way.我知道 SharedPreferences 的存在,但我想这样做。 If anyone can show me how (and why) I should handle this, it would be greatly appreciated.如果有人能告诉我应该如何(以及为什么)处理这个问题,将不胜感激。

Objects cannot be saved in firebase directly.对象不能直接保存在firebase中。 If it is Model you can store it in map format.如果它是 Model 你可以将它存储为 map 格式。 In your case shore the them mode as String or int ie 0 for light and 1 for dark and convert it when you read the data.在您的情况下,将它们的模式支持为 String 或 int,即 0 表示亮,1 表示暗,并在读取数据时将其转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用嵌套的 map 字段为带有 flutter 的云 Firestore 创建用户 model? - How to create user model with nested map field for cloud firestore with flutter? 新项目的 Cloud Firestore 默认位置错误 - Cloud Firestore default location error on new project flutter中如何将用户数据保存在cloud fire store - How to save user data in cloud fire store in flutter 注册后如何将用户凭据存储在 firestore 中 - How to store user credential in firestore after they have signup 如何在 Flutter web 应用程序中使用云 Firestore - How to use cloud firestore in Flutter web app Firebase Cloud Firestore - 如何导出和存储用户作为顶级买家的排名百分比 - Firebase Cloud Firestore - How to derive and store the percentage of a user's rank as a top buyer 如何将图像存储在 flutter 火库中并显示它 - How to store image in flutter firestore and display it 如何将来自 Firestore 的字段值存储在 Flutter 中? - How to store field value from firestore in Flutter? 运行 Cloud Function 将 Firebase 经过身份验证的用户数据存储到 Firestore - Run Cloud Function to store Firebase Auth'ed user data to Firestore Firebase Cloud Firestore:在集合中为每个 UID 创建一个新文档 - Firebase Cloud Firestore: Creating a new document in a collection for every UID
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM