[英]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.