Daniyal Haider
Daniyal Haider

Reputation: 33

data variables returns to 0 after changing them in one file

i am building a quiz app and it stores many values like taps, correct answer and time and many more. Now at the end i want a scoreboard that prints the values of these variable. I am using a dart file where it stores such information and uses that file in scoreboard. The problem i am having is that the data i need to store first is working. it is getting the data and storing in the data class. But when i call the class back at score board it does not shows the values rather prints null values.

here is my scoreboard

import 'package:dyslexia/pages/data/DataQ1Shape.dart';
import 'package:dyslexia/pages/data/DataQ2Shape.dart';
import 'package:dyslexia/pages/data/DataQ3Shape.dart';
import 'package:dyslexia/pages/data/DataQ4Shape.dart';
import 'package:dyslexia/pages/data/DataQ5Shape.dart';
import 'package:dyslexia/pages/data/DataQ6Shape.dart';

import 'package:flutter/material.dart';

class ScoreBoardL1R1 extends StatefulWidget {
  const ScoreBoardL1R1({Key? key}) : super(key: key);

  @override
  State<ScoreBoardL1R1> createState() => _ScoreBoardL1R1State();
}

class _ScoreBoardL1R1State extends State<ScoreBoardL1R1> {
  late int clicksPerpage;
  late int correctAns;
  late int incorrectAns;
  late int timeFirstClick;
  late num duration;
  late List timeIntervalClicks;

  int timer = 0;
  bool canceltimer = false;
  double Accuracy = 0;
  var efficiency = 0;
  double Missrate = 0;
  List timeFC = [];
  List Duration = [];
  double AverageClickTime = 0;
  DataQ1Shape data1 = DataQ1Shape();
  DataQ2Shape data2 = DataQ2Shape();
  DataQ3Shape data3 = DataQ3Shape();
  DataQ4Shape data4 = DataQ4Shape();
  DataQ5Shape data5 = DataQ5Shape();
  DataQ6Shape data6 = DataQ6Shape();

  mappdata() {
    clicksPerpage = (data1.clicksPerpage +
            data2.clicksPerpage +
            data3.clicksPerpage +
            data4.clicksPerpage +
            data5.clicksPerpage +
            data6.clicksPerpage)
        .toInt();
    correctAns = (data1.correctAns +
            data2.correctAns +
            data3.correctAns +
            data4.correctAns +
            data5.correctAns +
            data6.correctAns)
        .toInt();
    incorrectAns = (data1.incorrectAns +
            data2.incorrectAns +
            data3.incorrectAns +
            data4.incorrectAns +
            data5.incorrectAns +
            data6.incorrectAns)
        .toInt();
    Accuracy = correctAns / clicksPerpage;
    Missrate = incorrectAns / clicksPerpage;
    efficiency = correctAns * clicksPerpage;
    timeFC = [
      data1.timeFirstClick,
      data2.timeFirstClick,
      data3.timeFirstClick,
      data4.timeFirstClick,
      data5.timeFirstClick,
      data6.timeFirstClick,
    ];
    Duration = [
      data1.duration,
      data2.duration,
      data3.duration,
      data4.duration,
      data5.duration,
      data6.duration
    ];

    AverageClickTime = (data1.duration +
            data2.duration +
            data3.duration +
            data4.duration +
            data5.duration +
            data6.duration) /
        clicksPerpage;
  }

  bool printresult = false;
  @override
  void initState() {
    super.initState();
    mappdata();
  }

  void checkdata() {
    if (data1.clicksPerpage != 0 &&
        data2.clicksPerpage != 0 &&
        data3.clicksPerpage != 0 &&
        data4.clicksPerpage != 0 &&
        data5.clicksPerpage != 0 &&
        data6.clicksPerpage != 0) {
      setState(() {
        printresult = true;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        automaticallyImplyLeading: true,
        backgroundColor: Colors.cyan,
        title: Text("AD&DY"),
      ),
      body: Padding(
        padding: const EdgeInsets.all(14.0),
        child: SingleChildScrollView(
            child: (printresult)
                ? Center(
                    child: Column(children: [
                      SizedBox(
                          height: MediaQuery.of(context).size.height * 0.02),
                      SizedBox(
                        height: 50,
                        width: 400,
                        child: Column(children: [
                          Text(
                            "Clicks Per page  $clicksPerpage",
                            style: TextStyle(color: Colors.black),
                          )
                        ]),
                      ),
                      SizedBox(
                          height: MediaQuery.of(context).size.height * 0.02),
                      SizedBox(
                        height: 50,
                        width: 400,
                        child: Row(children: [
                          Text(
                            "CorrectAnswers  $correctAns",
                            style: TextStyle(color: Colors.black),
                          )
                        ]),
                      ),
                      SizedBox(
                          height: MediaQuery.of(context).size.height * 0.02),
                      SizedBox(
                        height: 50,
                        width: 400,
                        child: Row(children: [
                          Text(
                            "Incorrect Answers  $incorrectAns",
                            style: TextStyle(color: Colors.black),
                          )
                        ]),
                      ),
                      SizedBox(
                          height: MediaQuery.of(context).size.height * 0.02),
                      SizedBox(
                        height: 100,
                        width: 400,
                        child: Row(children: [
                          Text(
                            "Accuracy  $Accuracy",
                            style: TextStyle(color: Colors.black),
                          )
                        ]),
                      ),
                      SizedBox(
                          height: MediaQuery.of(context).size.height * 0.02),
                      SizedBox(
                        height: 100,
                        width: 400,
                        child: Row(children: [
                          Text(
                            "Effiecency  $efficiency",
                            style: TextStyle(color: Colors.black),
                          )
                        ]),
                      ),
                      SizedBox(
                          height: MediaQuery.of(context).size.height * 0.02),
                      SizedBox(
                        height: 100,
                        width: 400,
                        child: Row(children: [
                          Text(
                            "Missrate  $Missrate",
                            style: TextStyle(color: Colors.black),
                          )
                        ]),
                      ),
                      SizedBox(
                          height: MediaQuery.of(context).size.height * 0.02),
                      SizedBox(
                        child: Row(children: [
                          Text(
                            "Time to First Clicks: \nQ1 : " +
                                timeFC[0].toString() +
                                "\nQ2 : " +
                                timeFC[1].toString() +
                                "\nQ3 : " +
                                timeFC[2].toString() +
                                "\nQ4 : " +
                                timeFC[3].toString() +
                                "\nQ5 : " +
                                timeFC[4].toString() +
                                "\nQ6 : " +
                                timeFC[5].toString() +
                                "\n",
                            style: TextStyle(color: Colors.black),
                          )
                        ]),
                      ),
                      SizedBox(
                          height: MediaQuery.of(context).size.height * 0.02),
                      SizedBox(
                        child: Row(children: [
                          Text(
                            "Duration of each Question: \nQ1 : " +
                                Duration[0].toString() +
                                "\nQ2 : " +
                                Duration[1].toString() +
                                "\nQ3 : " +
                                Duration[2].toString() +
                                "\nQ4 : " +
                                Duration[3].toString() +
                                "\nQ5 : " +
                                Duration[4].toString() +
                                "\nQ6 : " +
                                Duration[5].toString() +
                                "\n",
                            style: TextStyle(color: Colors.black),
                          )
                        ]),
                      ),
                      SizedBox(
                          height: MediaQuery.of(context).size.height * 0.02),
                      SizedBox(
                        height: 100,
                        width: 400,
                        child: Row(children: [
                          Text(
                            "Average Click time $AverageClickTime",
                            style: TextStyle(color: Colors.black),
                          )
                        ]),
                      ),
                    ]),
                  )
                : CircularProgressIndicator()),
      ),
    );
  }
}

here is my data file in which i store data being recorded.

    class DataQ1Shape {
  int clicksPerpage;
  int correctAns;
  int incorrectAns;
  int timeFirstClick;
  num duration;
  List? timeIntervalClicks;

  DataQ1Shape({
    this.clicksPerpage = 0,
    this.correctAns = 0,
    this.incorrectAns = 0,
    this.duration = 0,
    this.timeFirstClick = 0,
    this.timeIntervalClicks,
  });
}

here is my Question page on where data is reading and writing

import 'dart:async';
import 'package:dyslexia/pages/data/DataQ1Shape.dart';
import 'package:dyslexia/pages/level1/round1/Q2shape.dart';
import 'package:flutter/material.dart';
import 'package:flutter_tabler_icons/flutter_tabler_icons.dart';

import '../../../utilities/QuestionWidget.dart';

class Q1Shape extends StatefulWidget {
  const Q1Shape({Key? key}) : super(key: key);

  @override
  State<Q1Shape> createState() => _Q1ShapeState();
}

class _Q1ShapeState extends State<Q1Shape> {
  String question = "Match the shape that you can see with the given options";
  var changeButton = false;
  bool changecolourC = false;
  bool changecolourW1 = false;
  bool changecolourW2 = false;
  bool changecolourW3 = false;

  bool showImage = false;
  double elev = 12;
  bool showoptions = false;
  bool showquestion = true;

  int clicksPerpage = 0;
  int correctAns = 0;
  int incorrectAns = 0;
  int timeFirstClick = 0;
  num duration = 0;
  List timeIntervalClicks = [];

  int timer = 0;
  bool canceltimer = false;

  void starttimer() async {
    const onesec = Duration(seconds: 1);
    Timer.periodic(onesec, (Timer t) {
      if (mounted) {
        setState(() {
          timer = timer + 1;
        });
      }
    });
  }

  num countDuration() {
    for (int i = 0; i < timeIntervalClicks.length; i++) {
      duration = duration + timeIntervalClicks[i];
    }
    return duration;
  }

  DataQ1Shape data = DataQ1Shape();

  void mappData() {
    data.clicksPerpage = clicksPerpage;
    data.correctAns = correctAns;
    data.incorrectAns = incorrectAns;
    data.timeFirstClick = timeFirstClick;
    data.timeIntervalClicks = timeIntervalClicks;
    data.duration = countDuration();
  }

  @override
  void initState() {
    starttimer();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final Size size = MediaQuery.of(context).size;

    return Scaffold(
      appBar: AppBar(
        automaticallyImplyLeading: true,
        backgroundColor: Colors.cyan,
        title: Text("AD&DY"),
      ),
      body: Padding(
          padding: const EdgeInsets.all(14.0),
          // ignore: prefer_const_literals_to_create_immutables
          child: SingleChildScrollView(
              child: Column(children: [
           
            showquestion
                ? Column(
                    children: [
                      QuestionWidget(question: question),
                      const SizedBox(
                        height: 20,
                      ),
                      showImage
                          ? Column(
                              children: [
                                CustomPaint(
                                  size: Size(200, 150),
                                  painter: RPSCustomPainterQ(),
                                ),
                              ],
                            )
                          : SizedBox(
                              height: 0,
                            ),
                      ElevatedButton(
                          onPressed: () {
                            clicksPerpage += 1;
                            timeFirstClick = timer.toInt();

                            timeIntervalClicks.add(timer.toInt());
                            if (mounted) {
                              setState(() {
                                timer = 0;
                              });
                            }

                            Timer(Duration(seconds: 0), () {
                              if (mounted) {
                                setState(() {
                                  showImage = true;
                                });
                              }
                            });
                            Timer(Duration(seconds: 5), () {
                              if (mounted) {
                                setState(() {
                                  showImage = false;
                                  showquestion = false;
                                  showoptions = true;
                                });
                              }
                            });
                          },
                          child: Text('show image')),
                    ],
                  )
                : SizedBox(height: MediaQuery.of(context).size.height * 0.1),
            showoptions
                ? Column(
                    children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        children: [
                          Material(
                            elevation: elev,
                            shadowColor: Colors.grey,
                            color:
                                changecolourW1 ? Colors.red[100] : Colors.cyan,
                            borderRadius:
                                BorderRadius.circular(changeButton ? 50 : 8),
                            child: InkWell(
                              onTap: () {
                                changecolourW1 = true;
                                timeIntervalClicks.add(timer.toInt());
                                clicksPerpage += 1;
                                incorrectAns += 1;
                                elev = 0;
                                if (mounted) {
                                  setState(() {
                                    timer = 0;
                                  });
                                }
                              },
                              child: Container(
                                height: 120,
                                width: 180,
                                alignment: Alignment.topLeft,
                                child: CustomPaint(
                                  size: Size(size.width, size.height),
                                  painter: RPSCustomPainterO1(),
                                ),
                              ),
                            ),
                          ),
                          Material(
                            elevation: elev,
                            shadowColor: Colors.grey,
                            color:
                                changecolourC ? Colors.green[200] : Colors.cyan,
                            borderRadius:
                                BorderRadius.circular(changeButton ? 50 : 8),
                            child: InkWell(
                              onTap: () {
                                changecolourC = true;
                                timeIntervalClicks.add(timer.toInt());
                                clicksPerpage += 1;
                                correctAns += 1;
                                changecolourC = true;
                                if (mounted) {
                                  setState(() {
                                    timer = 0;
                                  });
                                }

                                elev = 0;
                              },
                              child: Container(
                                height: 120,
                                width: 180,
                                alignment: Alignment.topLeft,
                                child: CustomPaint(
                                  size: Size(size.width, size.height),
                                  painter: RPSCustomPainterQ(),
                                ),
                              ),
                            ),
                          ),
                        ],
                      ),
                      SizedBox(
                          height: MediaQuery.of(context).size.height * 0.1),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        children: [
                          Material(
                            elevation: elev,
                            shadowColor: Colors.grey,
                            color:
                                changecolourW2 ? Colors.red[100] : Colors.cyan,
                            borderRadius:
                                BorderRadius.circular(changeButton ? 50 : 8),
                            child: InkWell(
                              onTap: () {
                                changecolourW1 = true;
                                timeIntervalClicks.add(timer.toInt());
                                clicksPerpage += 1;
                                incorrectAns += 1;
                                changecolourW2 = true;
                                if (mounted) {
                                  setState(() {
                                    timer = 0;
                                  });
                                }

                                elev = 0;
                              },
                              child: Container(
                                height: 120,
                                width: 180,
                                alignment: Alignment.topLeft,
                                child: CustomPaint(
                                  size: Size(size.width, size.height),
                                  painter: RPSCustomPainterO2(),
                                ),
                              ),
                            ),
                          ),
                          Material(
                            elevation: elev,
                            shadowColor: Colors.grey,
                            color:
                                changecolourW3 ? Colors.red[100] : Colors.cyan,
                            borderRadius:
                                BorderRadius.circular(changeButton ? 50 : 8),
                            child: InkWell(
                              onTap: () {
                                changecolourW1 = true;
                                timeIntervalClicks.add(timer.toInt());
                                clicksPerpage += 1;
                                incorrectAns += 1;
                                changecolourW3 = true;
                                if (mounted) {
                                  setState(() {
                                    timer = 0;
                                  });
                                }

                                elev = 0;
                              },
                              child: Container(
                                height: 120,
                                width: 180,
                                alignment: Alignment.topLeft,
                                child: CustomPaint(
                                  size: Size(size.width, size.height),
                                  painter: RPSCustomPainterO3(),
                                ),
                              ),
                            ),
                          ),
                        ],
                      ),
                      SizedBox(
                          height: MediaQuery.of(context).size.height * 0.1),
                      Material(
                        color: Colors.black,
                        borderRadius:
                            BorderRadius.circular(changeButton ? 50 : 8),
                        child: InkWell(
                          onTap: () {
                            mappData();
                            print(data.clicksPerpage);
                            Duration(seconds: 4);
                            Navigator.of(context).pushReplacement(
                                MaterialPageRoute(
                                    builder: (context) => Q2Shape()));
                          },
                          child: AnimatedContainer(
                            duration: Duration(seconds: 4),
                            width: changeButton ? 50 : 150,
                            height: 50,
                            alignment: Alignment.center,
                            child: changeButton
                                ? Icon(
                                    Icons.done,
                                    color: Colors.black,
                                  )
                                : Text(
                                    'Next',
                                    style: TextStyle(
                                        color: Colors.white,
                                        fontWeight: FontWeight.bold,
                                        fontSize: 18),
                                  ),
                          ),
                        ),
                      ),
                    ],
                  )
                : SizedBox(height: MediaQuery.of(context).size.height * 0.1),
          ]))),
    );
  }
}

Upvotes: 1

Views: 25

Answers (0)

Related Questions