首页 > 解决方案 > 在 userQuestion 和 userAnswer 中出现错误

问题描述

https://www.youtube.com/watch?v=GYYTQcohNwA从此视频制作计算器并在 userQuestion 和 userAnswer 上出错。我已经正确地遵循它并努力解决这个问题,但仍然没有办法解决这个问题。请帮忙!!行号 18 和 26 是问题所在。

什么是飞镖?去年 12 月,谷歌首次发布了 Flutter 1.0,此前它处于 beta 模式超过 18 个月。Dart 是用于编写 Flutter 应用程序的编程语言。Dart 是 Google 的另一款产品,在 Flutter 之前于 11 月发布了 2.1 版。刚开始时,Flutter 社区并不像 ReactNative、Ionic 或 Xamarin 那样广泛。

不久前,我发现自己喜欢 JavaScript。能够为我的实习开发 ReactNative 移动应用程序让我欣喜若狂。我也喜欢编写混合移动应用程序,所以想尝试一下 Flutter,就像我去年某个时候做过 Xamarin 一样。

在我第一眼看到 Flutter(和 Dart)的时候,我感到很困惑,似乎什么都不懂。他们甚至在他们的文档中为从 React Native 迁移的开发人员提供了一个部分。因此,我开始深入挖掘 Dart 的所有内容。

Dart 看起来有点像 C,是一种面向对象的编程语言。因此,如果您更喜欢 C 语言或 Java,那么 Dart 就是您的最佳选择,而且您很可能会精通它。

Dart 不仅用于移动应用程序开发,还是一种编程语言。它被 Ecma (ECMA-408) 批准为标准,用于构建 Web、服务器、桌面,当然还有移动应用程序上的任何东西(是的,就是标准化我们最喜欢的 ES5 和 ES6 的人。)

import 'package:flutter/material.dart';
import 'buttons.dart';
import 'package:math_expressions/math_expressions.dart';

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

bool isOperator(String x) {
  if (x == '%' || x == '/' || x == 'x' || x == '+' || x == '-' || x == '=') {
    return true;
  }
  return false;
}

void equalPressed() {
  String finalQuestion = userQuestion;
  finalQuestion = finalQuestion.replaceAll('x', '*');

  Parser p = Parser();
  Expression exp = p.parse(finalQuestion);
  ContextModel cm = ContextModel();
  double eval = exp.evaluate(EvaluationType.REAL, cm);

 userAnswer = eval.toString();
}

class _HomePageState extends State<HomePage> {
  var userQuestion = '';
  var userAnswer = '';

  final List<String> buttons = [
    'C',
    'DEL',
    '%',
    '/',
    '7',
    '8',
    '9',
    'x',
    '4',
    '5',
    '6',
    '-',
    '1',
    '2',
    '3',
    '+',
    '0',
    '00',
    '.',
    '=',
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color(0xFFE5CCFF),
      body: Column(
        children: <Widget>[
          Expanded(
            child: Container(
              child: Column(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: <Widget>[
                    SizedBox(
                      height: 50,
                    ),
                    Container(
                      padding: EdgeInsets.all(20),
                      alignment: Alignment.centerLeft,
                      child: Text(
                        userQuestion,
                        style: TextStyle(fontSize: 20),
                      ),
                    ),
                    Container(
                      padding: EdgeInsets.all(20),
                      alignment: Alignment.centerRight,
                      child: Text(
                        userAnswer,
                        style: TextStyle(fontSize: 20),
                      ),
                    ),
                  ]),
            ),
          ),
          Expanded(
            flex: 2,
            child: Container(
              child: GridView.builder(
                  itemCount: buttons.length,
                  gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                      crossAxisCount: 4),
                  itemBuilder: (BuildContext context, int index) {
                    // clear button
                    if (index == 0) {
                      return MyButton(
                        buttonTapped: () {
                          setState(() {
                            userQuestion = '';
                          });
                        },
                        buttonText: buttons[index],
                        color: Colors.green,
                        textColor: Colors.white,
                      );
                    } else if (index == 1) {
                      // Delete Button
                      return MyButton(
                        buttonTapped: () {
                          setState(() {
                            userQuestion = userQuestion.substring(
                                0, userQuestion.length-1);
                          });
                        },
                        buttonText: buttons[index],
                        color: Colors.red,
                        textColor: Colors.white,
                      );
                    }
                    // equal button
                    else if (index == buttons.length-1) {
                      return MyButton(
                        buttonTapped: () {
                          setState(() {
                            equalPressed();
                          });
                        },
                        buttonText: buttons[index],
                        color: Colors.deepPurple,
                        textColor: Colors.white,
                      );
                    } else {
                      // Rest of the Button
                      return MyButton(
                        buttonTapped: () {
                          setState(() {
                            userQuestion += buttons[index];
                          });
                        },
                        buttonText: buttons[index],
                        color: isOperator(buttons[index])
                            ? Colors.deepPurple
                            : Colors.deepPurple[50],
                        textColor: isOperator(buttons[index])
                            ? Colors.white
                            : Colors.deepPurple,
                      );
                    }
                  }),
            ),
          ),
        ],
      ),
    );
  }
}


标签: flutterdartcalculator

解决方案


设置方法isOperatorequalPressed你的 _HomePageState


推荐阅读