I'm making a screen with creating a pin code to enter the application. But unfortunately my code doesn't work.
I'm trying to implement this through local storage. Here's the logic - as soon as the user goes to the create screen, I check if there is a pin code in the local storage. Here is the function -
Future<int?> sharedPrefsGet() async {
final prefs = await SharedPreferences.getInstance();
print(await prefs.getInt('pinCode'));
return await prefs.getInt('pinCode');
}
late final Future<int?> future = sharedPrefsGet();
void initState() {
super.initState();
sharedPrefsGet();
}
Further, when entering the pin code, I check if there is a pin code and what will happen -
onCompleted: (value) async {
pinCode = int parse(value);
sharedPrefsSet(pinCode);
if(sharedPrefsGet() == value) {
context.go('/home');
} else {
context.go('/createPinCode');
}
context.go('/createPinCode');
},
and of course, there I try to save the pin code to the local storage. Here is the function -
var pinCode;
sharedPrefsSet(int pin) async {
final prefs = await SharedPreferences.getInstance();
return await prefs.setInt('pinCode', pin);
}
but my code doesn't work, I don't get any errors.
The user enters the password, this screen opens again, he enters the code again, but navigation to the home page no longer occurs. Why?
full code -
class CreatePinCode extends StatefulWidget {
@override
_CreatePinCodeState createState() => _CreatePinCodeState();
}
class _CreatePinCodeState extends State<CreatePinCode> {
final TextEditingController _controller = TextEditingController();
var pinCode;
sharedPrefsSet(int pin) async {
final prefs = await SharedPreferences.getInstance();
return await prefs.setInt('pinCode', pin);
}
Future<int?> sharedPrefsGet() async {
final prefs = await SharedPreferences.getInstance();
print(await prefs.getInt('pinCode'));
return await prefs.getInt('pinCode');
}
late final Future<int?> future = sharedPrefsGet();
@override
void initState() {
super.initState();
sharedPrefsGet();
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: ConfigColor.background,
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
FutureBuilder(
future: future,
builder: (context, snapshot) =>
snapshot.hasData? Text('123123123123123') : Text('ababababab')
),
Padding(
padding: const EdgeInsets.all(100),
child: SizedBox(
height: 70,
child: Center(
child: PinCodeTextField(
controller: _controller,
mainAxisAlignment: MainAxisAlignment.spaceAround,
obscuringWidget: Container(
width: 15,
height: 15,
decoration: BoxDecoration(
color: ConfigColor.green,
shape: BoxShape.circle
),
),
appContext: context,
length: 4,
onChanged: (value) {
print(value);
},
pinTheme: PinTheme(
shape: PinCodeFieldShape.box,
borderRadius: BorderRadius.circular(50),
fieldHeight: 15,
fieldWidth: 15,
activeColor: ConfigColor.green,
inactiveColor: Colors.white.withOpacity(0.3),
disabledColor: Colors.white.withOpacity(0.3),
),
onCompleted: (values) async {
final prefs = await SharedPreferences.getInstance();
final pinCod = await prefs.setInt('pinCod', int.parse(values));
if(!pinCod) {// either true or false
if(pinCod == int.parse(values)) {
log('Все хорошо');
} else {
log('Код не совпадает!');
}
}
pinCode = int.parse(values);
final int? value = await sharedPrefsGet() ;
if(value== null) {
context.go('/createPinCode');
} else if (value == pinCode){
await sharedPrefsSet(pinCode);
context.go('/home');
} else {
}
},
)
),
),
),
// implement the custom NumPad
NumPad(
buttonSize: 65,
buttonColor: ConfigColor.background,
iconColor: Colors.deepOrange,
controller: _controller,
delete: () {
_controller.text = _controller.text
.substring(0, _controller.text.length - 1);
},
// do something with the input numbers
onSubmit: () {
debugPrint('Your code: ${_controller.text}');
showDialog(
context: context,
builder: (_) => AlertDialog(
content: Text(
"You code is ${_controller.text}",
style: const TextStyle(fontSize: 30),
),
));
},
),
],
)
);
}
}
CodePudding user response:
It would better add return type on sharedPrefsGet
while it is returning a future.
Future<int> sharedPrefsGet() async {
Using Future builder to get data.
sharedPrefsSet(int pin) async {
final prefs = await SharedPreferences.getInstance();
await prefs.setInt('pinCode', pin);
}
Future<int?> sharedPrefsGet() async {
final prefs = await SharedPreferences.getInstance();
print(await prefs.getInt('pinCode'));
return await prefs.getInt('pinCode');
}
late final Future<int?> future = sharedPrefsGet();
@override
Widget build(BuildContext context) {
return Scaffold(
body: FutureBuilder(
future: future,
builder: (context, snapshot) =>
snapshot.hasData?
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
!snapshot.hasData ? Text('ababababab') : Text('123123123'),
.........
And use await to get data
onCompleted: (value) async {
final prefs = await SharedPreferences.getInstance();
final pinCod = await prefs.setInt('pinCod', int.parse(value));
if(!pinCod) {// either true or false
if(pinCod == int.parse(value)) {
log('Все хорошо');
} else {
log('Код не совпадает!');
}
}
pinCode = int.parse(value);
final int? value = await sharedPrefsGet() ;
if(value== null) {
} else if (value== youValue){
await sharedPrefsSet(pinCode);
} else {
}
},
CodePudding user response:
problem is here :
sharedPrefsGet() async {
final prefs = await SharedPreferences.getInstance();
print(await prefs.getInt('pinCode'));
return await prefs.getInt('pinCode');
}
when you are doing this : prefs.getInt('pinCode')
Instead of this Try to do this :
sharedPrefsGet() async {
final prefs = await SharedPreferences.getInstance();
print(await prefs.getInt('pinCode') ?? -1);
return await prefs.getInt('pinCode') ?? -1; //here -1 means not given pin
}
now add your code for this logic