I have a Flutter application and I want to add a page that appears when opening the application asking the user to answer a question such as how many countries in the world - the answer is already stored in the application, so that if the answer is correct, the answer is stored and the application opens and this page does not appear again, But if the answer is wrong, the user remains On this page, he cannot open the application until he writes the correct answer Any suggestions or examples that would be helpful?
CodePudding user response:
you would check the user's answer, if it's correct, you save a boolean in the shared preferences and then navigate to the app home page and every time you open the app you check on this boolean from the shared preferences, if it's true then don't show the question and open the home page directly, if not, then show the question again
CodePudding user response:
In a very simple way, you can use the SharedPreferences plugin to store the answer to your question permanently, for example
You can store a "question" key that will have the value "how many countries are there in the world?" (optional). You also store an "answer" key with the value "324" (the exact number of countries in the world)
Then you create an "answer_found" key which will be a boolean and will update if yes or no the user answers the question correctly.
Then when the application starts, you will first query the "answer_found" key to see if its value is True.
If this value is True, you do not display the questionnaire page, if it is false or null, you display the questionnaire page.
When the user will enter the answer, simply compare there his answer to the one contained in the "answer" key in the preferences. If it is correct, simply update the key "answer_found" to become true. In the opposite case do nothing (or what you want)
CodePudding user response:
I hope you will have the splash screen as the first screen to load in your application.
now, when the first screen will load. make this first screen a stateful.
this will allow you to use the "initstate" method. and you know that the initstate method will be called first to execute the code.
initstate(){
/// in this case I am using "get_storage" to store the data in local.
GetStorage prefs = GetStorage("ApplicationName");
bool isAnswered = prefs.read("isAnsweredQuestion") ?? false;
if(isAnswered){
/// redirect to the other screen.
}else{
/// redirect to the screen where you have the questions
/// or open the dialog having questions.
}
super.initstate();
};
the initstate method will execute for the first time when the application loads and execute the splash screen and it will check for the local data store.
if the user is opening the application for the first time then the local data will be null and we have used the null-check operator to handle that.
If the user already answered the question then we will get "true" stored in the local. and we will redirect the user to the other screen in that case.