gpt4 book ai didi

flutter - 在 flutter (dart) 中使用模型类

转载 作者:行者123 更新时间:2023-12-01 21:55:15 26 4
gpt4 key购买 nike

我正在尝试使用一些方法创建一个模型类,以简化从 Firebase 检索信息的过程。

我有一个“未注册”页面,用户在该页面上单击“使用 google 登录”按钮。如果我的用户已经注册,它会检查数据库以查看他是否完成了他的个人资料,否则它会使用 profileCompleted: false 字段将基本数据写入数据库并将用户重定向到个人资料页面,以便他可以完成他的个人资料。

我在这里尝试使用模型,但遇到了各种错误(刚开始学习 flutter/dart)。

我将分享一些代码,如果不够请告诉我!

未注册页面。

    if (user != null) {
await prefs.setString('id', user.uid);
// Check is already sign up
final QuerySnapshot result = await Firestore.instance
.collection('users')
.where('id', isEqualTo: user.uid)
.getDocuments();
final List<DocumentSnapshot> documents = result.documents;

if (documents.length == 0) {
debugPrint("User not in DB ?");
//debugPrint(userInfo.toString());
// Update data to server if new user

Firestore.instance.collection('users').document(user.uid).setData({
'id': user.uid,
'nickname': user.displayName,
'photoUrl': user.photoUrl,
'email': user.email,
'createdAt': DateTime.now(),
'provider': user.providerId,
'profileCompleted': false,
'bloodType': 'A',
'rhType': 'Negativ',
'donatedBefore': 'Nu',
'lastDonation': '0'
});
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => CompleteProfile(
currentUserId: user.uid,
userInfo: documents.single,
)));
} else if (documents.single["profileCompleted"] == false) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => CompleteProfile(
currentUserId: user.uid,
userInfo: documents.single,
)));
} else {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => HomePage(
currentUserId: user.uid,
userInfo: documents.single,
)));
}
}

完成个人资料页面

class CompleteProfile extends StatefulWidget {
final String currentUserId;
final userInfo;
static const routeName = '/profile';
final User user;

CompleteProfile(
{Key key,
this.title,
@required this.currentUserId,
@required this.userInfo,
this.user})
: super(key: key);

final String title;

@override
_CompleteProfileState createState() => _CompleteProfileState(user,
currentUserId: this.currentUserId, userInfo: this.userInfo);
}

class _CompleteProfileState extends State<CompleteProfile> {
User user;

_CompleteProfileState(this.user,
{Key key, @required this.currentUserId, @required this.userInfo});
final String currentUserId;
final userInfo;

我的问题是,如果我尝试设置这样的下拉菜单 ->

child: ListTile(
title: DropdownButton<String>(
items: _bloodTypes.map((String value) {
return DropdownMenuItem<String>(
value: value, child: Text(value));
}).toList(),
style: textStyle,
value: retrieveBloodType(user.bloodType),
onChanged: (value) => updateBloodType(value),

和默认值->

  String retrieveBloodType(String value) {
return _bloodType;
}

我得到一个错误,我正在获取 null。

我的想法是用类似User user = new User(userInfo["id"],userInfo["email"]..... 其中 userInfo 是从 firebase 检索到的对象。但是,这会引发另一个错误,即在初始化器中只能访问静态成员。

我的模型非常简单,所以先睹为快(几行,不是整个模型)。

class User {
int _id;
String _nickname;
int get id => _id;
String get nickname => _nickname;
set bloodType(String newBloodType) {
if (newBloodType.length <= 50 && newBloodType != null) {
_bloodType = newBloodType;
}
}

关于 id 必须如何更改它才能工作的任何想法?我考虑使用模型的唯一原因是简化屏幕小部件(因此我可以将所有 firebase 逻辑添加到模型而不是小部件)。

最佳答案

这就是我的做法

import 'package:cloud_firestore/cloud_firestore.dart';

class User {
final String userName;
final String email;
final String name;
final String phoneNumber;
final String profilePictureUrl;
final Timestamp creationDate;

const User(
this.userName,
this.email,
this.name,
this.phoneNumber,
this.profilePictureUrl,
this.creationDate
);

factory User.fromDocument(DocumentSnapshot document) {
return User(
document['userName'],
document['email'],
document['name'],
document['phoneNumber'],
document['profilePictureUrl'],
document['creationDate']
);
}

关于flutter - 在 flutter (dart) 中使用模型类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58146158/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com