Flutter: отображение общего балла / уровня для пользователя

#firebase #flutter

#firebase #flutter

Вопрос:

У меня есть score , topicTotal и level я устанавливаю состояние, и я печатаю каждый из них. topicTotal Это итоговый результат, в котором все баллы суммируются друг с другом и level основаны на их topicTotal .

score поступает из Firebase для каждого вопроса. Я хочу рассчитать общий балл на основе суммы score , и я хочу отобразить уровень для пользователя в зависимости от общего балла.

Несмотря на то, что я могу печатать score , topicTotal и level я не могу отобразить эти значения для пользователя.

Как я могу отобразить эти значения для пользователя, и если я не могу отобразить их из состояния, как я могу получить и отобразить их, используя другой подход?

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

Ответ №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;