Flutter 与setInt的颤振共享首选项问题
我试图建立一个应用程序,其中包括3个角色,学生教师和家长。我考虑使用auth更改流和共享首选项实现身份验证。ie setInt('usertype',1)表示学生,2表示教师,3表示家长 这是我的学生注册屏幕,如果用户成功注册,我会将usertype设置为1,我也会对教师和家长注册屏幕执行相同的操作Flutter 与setInt的颤振共享首选项问题,flutter,Flutter,我试图建立一个应用程序,其中包括3个角色,学生教师和家长。我考虑使用auth更改流和共享首选项实现身份验证。ie setInt('usertype',1)表示学生,2表示教师,3表示家长 这是我的学生注册屏幕,如果用户成功注册,我会将usertype设置为1,我也会对教师和家长注册屏幕执行相同的操作 class StudentRegisterScreen extends StatefulWidget { final Function toggleView; StudentRegister
class StudentRegisterScreen extends StatefulWidget {
final Function toggleView;
StudentRegisterScreen({this.toggleView});
@override
_StudentRegisterScreenState createState() => _StudentRegisterScreenState();
}
class _StudentRegisterScreenState extends State<StudentRegisterScreen> {
final AuthService _authService = AuthService();
final _formkey = GlobalKey<FormState>();
final usertype = await SharedPreferences.getInstance();
//String name = '';
//String email = '';
//String password = '';
@override
Widget build(BuildContext context) {
//String _message = '';
Size size = MediaQuery.of(context).size;
return Scaffold(
backgroundColor: HexColor(studentPrimaryColour),
body: SafeArea(
child: SingleChildScrollView(
child: Form(
key: _formkey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
SizedBox(
height: 20.0,
),
HeadingText(
text: 'Register',
size: 60.0,
color: Colors.white,
),
Container(
height: 20.0,
child: HeadingText(
text: AuthService().message,
size: 20.0,
color: Colors.white,
),
),
SizedBox(
height: 25.0,
),
RoundedInputField(
hintText: 'Name',
validator: (val) =>
val.isEmpty ? 'Oops! you left this field empty' : null,
onChanged: (val) {
name = val;
},
),
SizedBox(
height: 5.0,
),
RoundedInputField(
hintText: 'Email',
validator: (val) => val.isEmpty ? 'enter an email' : null,
onChanged: (val) {
email = val;
},
),
SizedBox(
height: 5.0,
),
RoundedInputField(
hintText: 'Password',
validator: (val) =>
val.isEmpty ? 'atleast provide a password' : null,
boolean: true,
onChanged: (val) {
password = val;
}),
SizedBox(
height: 5.0,
),
Container(
margin: EdgeInsets.symmetric(vertical: 10),
width: size.width * 0.8,
child: ClipRRect(
borderRadius: BorderRadius.circular(29),
child: FlatButton(
padding:
EdgeInsets.symmetric(vertical: 20, horizontal: 40),
color: Colors.white,
onPressed: () async {
//
// if (_formkey.currentState.validate()) {
// print(email);
// print(password);
usertype.setInt('usertype',1);
// dynamic result =
// await _authService.registerWithEmailpasswd(
// email,
// password,
// name,
// );
//
},
child: HeadingText(
color: HexColor(studentPrimaryColour),
text: 'Register',
size: 12.0,
),
),
),
),
SizedBox(
height: 15.0,
),
InkWell(
onTap: () {
// Navigator.pop(context);
widget.toggleView();
},
child: HeadingText(
text: 'Already registered?',
color: Colors.white,
size: 10,
),
),
],
),
),
),
),
);
}
}
下面是我现在的包装器类,我想在继续之前将值打印到控制台上,我将实现用于显示不同用户屏幕的切换案例
class Welcomescreen extends StatefulWidget {
@override
_WelcomescreenState createState() => _WelcomescreenState();
}
class _WelcomescreenState extends State<Welcomescreen> {
SharedPreferences userDetails;
int usertype;
@override
void initState() {
// TODO: implement initState
super.initState();
checkUserType();
}
void checkUserType() async {
userDetails = await SharedPreferences.getInstance();
setState(() {
usertype = userDetails.getInt('usertype') ?? 0;
});
print(usertype);
}
@override
Widget build(BuildContext context) {
//final user = Provider.of<UserModel>(context);
return Body();
}
}
类Welcomescreen扩展StatefulWidget{
@凌驾
_WelcomescreenState createState()=>\u WelcomescreenState();
}
类_WelcomescreenState扩展状态{
SharedReferences用户详细信息;
int用户类型;
@凌驾
void initState(){
//TODO:实现initState
super.initState();
checkUserType();
}
void checkUserType()异步{
userDetails=等待SharedReferences.getInstance();
设置状态(){
usertype=userDetails.getInt('usertype')??0;
});
打印(用户类型);
}
@凌驾
小部件构建(构建上下文){
//最终用户=提供者(上下文);
返回体();
}
}
userType通过创建final userType=wait SharedPreference.getInstance()
,引用了未来值SharedPreference.getInstance()
,告诉代码调用userType将返回未来的SharedPreference.getInstance(),在您实际等待它之前,不会解析为SharedReference类(使用await不会做任何事情,因为您实际上不在asyn函数中)
试着这样称呼它:
onPressed: () async {
(await usertype).setInt('usertype',1);
// or maybe (userType).setInt('usertype',1);
}
onPressed: () async {
(await SharedPreference.getInstance()).setInt('usertype',1);
}
用户类型将被替换为您正在引用的值(等待SharedReference.getInstance()
)
对于该程序,它如下所示:
onPressed: () async {
(await usertype).setInt('usertype',1);
// or maybe (userType).setInt('usertype',1);
}
onPressed: () async {
(await SharedPreference.getInstance()).setInt('usertype',1);
}
首先,它需要等待Future,然后您可以实际调用它的方法
checkUserType
是async
,它应该是Future
而不是void
,您必须等待checkUserType
完成,因此您必须在内部调用它,而不是WidgetsBinding.instance.addPostFrameCallback()async=>Wait checkUserType())
由于initState
不是async
函数,因此这里的答案与调用setState
内部initState
相关,可以helpful@ikerfah尝试添加Future
和WidgetsBinding.instance.addPostFrameCallback((41;)async=>await checkUserType())代码>在init()内,但抛出相同的错误..```E/flatter(14753):[ERROR:flatter/lib/ui/ui\u dart\u state.cc(177)]未处理的异常:NoSuchMethodError:对null调用了方法“setInt”。E/flatter(14753):接收器:空E/flatter(14753):尝试调用:setInt(“用户类型”,1)```