【问题标题】:Flutter: Display the total score / level to the userFlutter:向用户显示总分/等级
【发布时间】:2021-02-22 17:31:17
【问题描述】:

我在状态中设置了scoretopicTotallevel,我正在打印它们中的每一个。 topicTotal 是所有分数相加的最终分数,level 是基于他们的topicTotal

score 来自 Firebase 的每个问题。我想根据score 的总和计算总分,我想根据总分向用户显示一个级别。

尽管我可以打印scoretopicTotallevel,但我无法向用户显示这些值。

如何向用户显示这些值,如果无法从状态中显示它们,如何使用不同的方法检索和显示它们?

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}',

【问题讨论】:

    标签: firebase flutter


    【解决方案1】:

    您可以使用构建器方法 像这样的

    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(),
       );
      },
    );
    

    【讨论】:

      【解决方案2】:

      我想你错过了输入set topicTotal

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

      当你尝试获取时,它实际上可以更新值。

      get topicTotal => _topicTotal;
      

      【讨论】:

        猜你喜欢
        • 2020-01-05
        • 1970-01-01
        • 2021-08-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-20
        • 1970-01-01
        • 2022-09-23
        相关资源
        最近更新 更多