Flutter: Display the total score / level to the user

Viewed 441

I have score, topicTotal and level set in the state and I am printing each of them. The topicTotal is the final score where all scores are added onto each other and the level is based on their topicTotal.

score is coming from Firebase for each question. I want to calculate a total score based on the sum of score and I want to display a level to the user depending on the total score.

Even though, I am able to print score, topicTotal and level, I cannot display these values to the user.

How can I display these values to the user and if I can't display them from the state, how can I retrieve and display them using a different approach?

class AssessmentState with ChangeNotifier {
  double _progress = 0;
  Options _selected;
  dynamic _score;
  dynamic _topicTotal;

  final PageController controller = PageController();
  var idx = 0;

  get progress => _progress;
  get selected => _selected;
  get score => _score;
  get topicTotal => _topicTotal;

  set progress(double newValue) {
    _progress = newValue;
    notifyListeners();
  }

  set selected(Options newValue) {
    _selected = newValue;
    notifyListeners();
  }

  set score(dynamic newValue) {
    var score = idx += newValue.score;
    _score = newValue;
    print(score);
    _score = newValue;
    notifyListeners();
  }

  set topicTotal(dynamic newValue) {
    var topicTotal = idx;
    print(topicTotal);
    if (topicTotal <= 300) {
      print('Level 1');
    } else if (topicTotal <= 900) {
      print('Level 2');
    } else if (topicTotal <= 1400) {
      print('Level 3');
    } else
      print('Level 4');
    notifyListeners();
  }
  final Assessment assessment;
  final Questions questions;
  final Options options;
  final Options optionSelected;
  WellDonePage({this.assessment, this.questions, this.options, this.optionSelected});

  @override
  Widget build(BuildContext context) {
    var state = Provider.of<AssessmentState>(context);

    return Padding(
      padding: EdgeInsets.all(20),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Text(
            'Well Done! You completed the ${assessment.title} Assessment. Your level for the ${assessment.title} Assessment is ${state.topicTotal}',
2 Answers

You can use builder method something like this

StreamBuilder<QuerySnapshot>(
 stream: Firestore.instance.collection('DriverList').snapshots(),
 builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
   if (!snapshot.hasData) return new Text('Loading...');
   return new ListView(
      children: snapshot.data.documents.map((DocumentSnapshot document) {
         return new ListTile(
            title: new Text(document['name']),
            subtitle: new Text(document['phone']),
         );
      }).toList(),
   );
  },
);

I think you miss type in set topicTotal

set topicTotal(dynamic newValue) {
  var topicTotal = idx; // this line should change to "_topicTotal = idx;"
  print(topicTotal);

which can actually update the value when you try to get.

get topicTotal => _topicTotal;
Related