当我尝试更新用户配置文件时,我总是在null上调用getter



你好,我是flutter的新手,我正试图为登录的用户做一个简单的用户配置文件屏幕,但我遇到了一些错误,第一个是因为当我尝试使用StreamBuilder((时,流没有从UserProvider((的getter获得任何数据(这就是我的BL所在的位置(,它一直说getCurrentUserData((被调用为null,所以我只是将其直接连接到UserService((nitState((TextFormField((用用户数据填充,并让fullNameControllerUserModel((获取数据时,返回的错误一直说fullName被调用为null!我该如何解决这个问题?有人能指出我在这里哪里出了问题吗?

第页。S我正在按照本教程构建这个。

我的StreamBuilder((连接到UserProvider:

return StreamBuilder<UserModel>(
stream: userProviderData.getCurrentUserData(),
builder: (context, snapshot) {})

我的StreamBuilder((直接连接到UserService:

Directly connected to UserService
return StreamBuilder<UserModel>(
stream: userService.getCurrentUser(),
builder: (context, snapshot) {})

UserService((类:

// Get User
Stream<UserModel> getCurrentUser() {
return _db.collection('users').doc(_auth.currentUser.uid).snapshots().map(
(user) => UserModel.fromJson(user.data()),
);
}
// Add or Update User info
Future<void> saveUser(UserModel user) {
final _options = SetOptions(merge: true);
return _db.collection('users').doc(user.userId).set(user.toMap(), _options);
}

UserProvider((类:

final userProvider = ChangeNotifierProvider<UserProvider>((ref) {
return;
});
class UserProvider with ChangeNotifier {
final userService = UserService();
String _userId;
String _fullName;
// Getters
String get userId => _userId;
String get fullName => _fullName;
Stream<UserModel> get getCurrentUserData => userService.getCurrentUser();
// Setters
set changeFullName(String fullName) {
_fullName = fullName;
notifyListeners();
}
// Functions
void loadUser(UserModel userModel) {
_userId = userModel.userId;
_fullName = userModel.fullName;
}
void updateUser() {
final _currentUser = UserModel(
userId: _userId,
fullName: _fullName,
);
userService.saveUser(_currentUser);
}
}

EditProfileScreen((

class EditProfileScreen extends StatefulWidget {
const EditProfileScreen({this.userModel});
final UserModel userModel;
@override
_EditProfileScreenState createState() => _EditProfileScreenState();
}
class _EditProfileScreenState extends State<EditProfileScreen> {
final _fullNameController = TextEditingController();
final _validator = Validator();
@override
void dispose() {
_fullNameController.dispose();
super.dispose();
}
@override
void initState() {
super.initState();
final userStream = context.read(userProvider);
if (widget.userModel != null) {
// Edit
_fullNameController.text = widget.userModel.fullName;
userStream.loadUser(widget.userModel);
}
}
@override
Widget build(BuildContext context) {
final userData = context.read(userProvider);
return Scaffold(
body: Column(
children: [
TextFormFiled(
hintText: ‘Full name’,
keyboardType: TextInputType.name,
controller: _fullNameController,
validator: (value) => _validator.validateFullName(
value,
),
onChanged: (value) {
userData.changeFullName = value;
debugPrint(value);
}
),
ElevatedButton(
onPressed: () {
userData.updateUser();
Navigator.of(context).pop();
},
child: const Text(‘Save’),
),
],
),
);
}
}

您忘记退货了吗?

final userProvider = ChangeNotifierProvider<UserProvider>((ref) {
return; //return a UserProvider()
});

相关内容

  • 没有找到相关文章

最新更新