Home > database >  My issue is with if else condition is not running correctly
My issue is with if else condition is not running correctly

Time:01-27

I want to run if else condition but it seems to be an error i don't know what the error.I also print Correct option it seems to be equal to selected value but it is not running my if condition.Please solve error, This is the output of correct option and selected option

this is my code:


ElevatedButton(onPressed:  (){
                      if(Selectedvalue==snapshot.data!.docs[randomIndexes[index]]["Correct"]){
                        correctAnswers  ;
                        print("correct");
                        print(Selectedvalue);
                      }
                      else{
                        print("incorrect");
                        print("Selected value:$Selectedvalue");
                        print("Correct:${snapshot.data!.docs[randomIndexes[index]]["Correct"]}");

                      }
                      Selectedvalue=0;
        num  ;
        qno  ;
        _pageController.nextPage(duration: Duration(milliseconds: 300), curve: Curves.easeIn);
  if (index == randomIndexes.length-1) {
Navigator.pushReplacement(context, MaterialPageRoute(builder: (_)=>show_result(result: correctAnswers.toString())));
  }
        },
        child: Text("Submit",style: TextStyle(color: Colors.yellowAccent),)),

CodePudding user response:

Can you check that value that you are getting from snapshot.data!.docs[randomIndexes[index]]["Correct"] is type of number or not

As condition will be false if you are trying to compare integer with string

for ex :-


void main() {
  String str = "1";
  int numberVar = 1;
  
  if(str == numberVar){
    print("equal");
  }else{
    print("not equal");
  }
}

output - not equal

in above example , both have same value but have different type so condition is getting false

if you have same issue as above example then here is solution for your code

replace this line

if(Selectedvalue==snapshot.data!.docs[randomIndexes[index]]["Correct"]){

with this

if(Selectedvalue==int.parse(snapshot.data!.docs[randomIndexes[index]]["Correct"])){

hope this will help you

  • Related