我在尝试从 firebase 获取有关用户的信息时遇到问题。我正在使用 flutter。 我的应用程序适用于两种类型的用户。 对于每种类型,我都有一个全局用户文件和模型文件
用户1:
final FirebaseAuth fAuth = FirebaseAuth.instance;
User? currentFirebaseUser;
UserModel? userModelCurrentInfo;
用户2:
final FirebaseAuth fAuth = FirebaseAuth.instance;
User2? currentFirebaseUser;
User2Model? user2ModelCurrentInfo;
每种类型用户的模型类别定义如下:
class UserModel
{
String? phone;
String? name;
String? id;
String? email;
String? user_type;
UserModel({this.phone, this.name, this.id, this.email});
UserModel.fromSnapshot(DataSnapshot snap)
{
phone = (snap.value as dynamic)["phone"];
name = (snap.value as dynamic)["name"];
id = snap.key;
email = (snap.value as dynamic)["email"];
user_type='user';
日期已完美保存在 firebase 中。我有两个节点,一个用于用户,第二个用于用户2
这是我的用户屏幕:
class TryScreen extends StatelessWidget {
final GlobalKey<ScaffoldState> sKey = GlobalKey<ScaffoldState>();
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.red,
appBar: AppBar(
backgroundColor: Colors.red,
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Padding(
padding: const EdgeInsets.only(bottom: 16.0),
child: Text(
'Hey'+ ' '+(userModelCurrentInfo!.name ?? '') +' ' + '👋' ,
style: TextStyle(
fontSize: 20.0.sp,
fontWeight: FontWeight.bold,
),
textAlign: TextAlign.center,
),
),
],
),
),
);
}}
使用我上面提供的用户模型。 我将提供 user2 模型:
class User2Model
{
String? phone;
String? name;
String? id;
String? email;
String? user_type;
User2Model({this.phone, this.name, this.id, this.email, this.user_type});
User2Model.fromSnapshot(DataSnapshot snap)
{
phone = (snap.value as dynamic)["phone"];
name = (snap.value as dynamic)["name"];
id = snap.key;
email = (snap.value as dynamic)["email"];
user_type='user2';
}
}
并尝试执行我对用户屏幕所做的操作,然后我收到错误!
有什么建议吗?有人面临同样的问题吗?
具体错误是什么?您还可以添加错误吗?