如何在推送到数据库之前跨多个小部件在本地存储数据?
How to Store Data Locally Across Multiple Widgets before Pushing to the Database?
为了给你一些背景信息,我正在 Flutter 上构建一个引导页面,用户将首先被引导填写他的用户名,然后他将被推送到下一个小部件、出生日期等。
我计划在用户从第一个小部件移动到最后一个小部件时将数据存储在本地,并且仅将累积数据作为 class 一起推送,用户(用户名:xyz,出生日期: 11/11/2001, 性别: 男, 兴趣: 网球).
我计划使用 Provider 包,在将用户推送到下一页之前,我将在本地存储数据,如下所示。但是,我不确定如何在 Provider class
中设置 _user 实例的各个属性
你能告诉我如何去做吗?或者有没有更好的方法在本地存储数据。
我为每个页面提供程序的代码片段如下所示:
class User {
String uid;
String name;
String email;
String username;
String status;
int state;
String profilePhoto;
User({
this.uid,
this.name,
this.email,
this.username,
this.status,
this.state,
this.profilePhoto,
});
}
class UserProvider {
User _user;
User get getUser => _user;
set setUsername(username){
_user.name = username;
}
set setEmail(email){
_user.email = email;
etc...
}
}
//username page
Provider.of<UserProvider>(context, listen:false).setUsername='johncena';
print(Provider.of<UserProvider>(context, listen:false).getUser.username);
//email page
Provider.of<UserProvider>(context, listen:false).setEmail='hello@gmail.com';
print(Provider.of<UserProvider>(context, listen:false).getUser.username);
//at the last page
Provider.of<UserProvider>(context, listen:false).setStatus='available';
print(Provider.of<UserProvider>(context, listen:false).getUser.status);
//then I'll push the _user instance data that I stored in UserProvider to the backend
但是,我目前收到如下所示的错误消息:
[VERBOSE-2:ui_dart_state.cc(177)] Unhandled Exception: NoSuchMethodError: The setter 'username=' was called on null.
Receiver: null
Tried calling: username="johncena"
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
#1 UserProvider.setUsername= (package:okepos/provider/user_provider.dart:13:11)
#2 _UsernameState.build.<anonymous closure> (package:okepos/screen/Onboarding/username.dart:76:32)
#3 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:993:19)
#4 _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:1111:38)
#5 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:183:24)
#6 TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:598:11)
#7 BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:287:5)
#8 BaseTapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:222:7)
#9 PrimaryPointerGes<…>
有人知道问题出在哪里吗?
您应该在 UserProvider
构造函数中创建 User
的实例!
这将解决错误。
它会是这样的:
class UserProvider {
User _user;
UserProvider() {
_user = User();
}
User get getUser => _user;
set setUsername(username){
_user.name = username;
}
set setEmail(email){
_user.email = email;
etc...
}
}
您也可以直接使用 User
class 作为提供商 class
为了给你一些背景信息,我正在 Flutter 上构建一个引导页面,用户将首先被引导填写他的用户名,然后他将被推送到下一个小部件、出生日期等。
我计划在用户从第一个小部件移动到最后一个小部件时将数据存储在本地,并且仅将累积数据作为 class 一起推送,用户(用户名:xyz,出生日期: 11/11/2001, 性别: 男, 兴趣: 网球).
我计划使用 Provider 包,在将用户推送到下一页之前,我将在本地存储数据,如下所示。但是,我不确定如何在 Provider class
中设置 _user 实例的各个属性你能告诉我如何去做吗?或者有没有更好的方法在本地存储数据。
我为每个页面提供程序的代码片段如下所示:
class User {
String uid;
String name;
String email;
String username;
String status;
int state;
String profilePhoto;
User({
this.uid,
this.name,
this.email,
this.username,
this.status,
this.state,
this.profilePhoto,
});
}
class UserProvider {
User _user;
User get getUser => _user;
set setUsername(username){
_user.name = username;
}
set setEmail(email){
_user.email = email;
etc...
}
}
//username page
Provider.of<UserProvider>(context, listen:false).setUsername='johncena';
print(Provider.of<UserProvider>(context, listen:false).getUser.username);
//email page
Provider.of<UserProvider>(context, listen:false).setEmail='hello@gmail.com';
print(Provider.of<UserProvider>(context, listen:false).getUser.username);
//at the last page
Provider.of<UserProvider>(context, listen:false).setStatus='available';
print(Provider.of<UserProvider>(context, listen:false).getUser.status);
//then I'll push the _user instance data that I stored in UserProvider to the backend
但是,我目前收到如下所示的错误消息:
[VERBOSE-2:ui_dart_state.cc(177)] Unhandled Exception: NoSuchMethodError: The setter 'username=' was called on null.
Receiver: null
Tried calling: username="johncena"
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
#1 UserProvider.setUsername= (package:okepos/provider/user_provider.dart:13:11)
#2 _UsernameState.build.<anonymous closure> (package:okepos/screen/Onboarding/username.dart:76:32)
#3 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:993:19)
#4 _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:1111:38)
#5 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:183:24)
#6 TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:598:11)
#7 BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:287:5)
#8 BaseTapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:222:7)
#9 PrimaryPointerGes<…>
有人知道问题出在哪里吗?
您应该在 UserProvider
构造函数中创建 User
的实例!
这将解决错误。
它会是这样的:
class UserProvider {
User _user;
UserProvider() {
_user = User();
}
User get getUser => _user;
set setUsername(username){
_user.name = username;
}
set setEmail(email){
_user.email = email;
etc...
}
}
您也可以直接使用 User
class 作为提供商 class