flutter 红色屏幕显示“无效双精度”

deyfvvtc  于 2023-04-07  发布在  Flutter
关注(0)|答案(2)|浏览(174)

大家好,我写的程序代码计算二度方程在Flutter和 dart 编程语言当我写的代码,我没有在分析器中出错,但错误出现在模拟器中的红色屏幕说无效的双我能做什么,这个问题在困惑从这个问题,并审查我的代码?

import 'dart:math';
import 'package:flutter/material.dart';

void main() {
  runApp(new MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Generated App',
      theme: new ThemeData(
        brightness: Brightness.dark,
        primarySwatch: Colors.blue,
        primaryColor: const Color(0xFF212121),
        accentColor: const Color(0xFF64ffda),
        canvasColor: const Color(0xFF303030),
      ),
      home: new MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  GlobalKey<FormState> formstate = GlobalKey();
  TextEditingController x2 = TextEditingController();
  TextEditingController x = TextEditingController();
  TextEditingController c = TextEditingController();
  @override
  Widget build(BuildContext context) {
    var a = x2.text;
    var b = x.text;
    var _c = c.text;
    var A = double.parse(a);
    var B = double.parse(b);
    var C = double.parse(_c);
    var _x1 = ((-1 * A) + sqrt((B * B) - (4 * A * C))) / (2 * A);
    var _x2 = ((-1 * A) - sqrt((B * B) - (4 * A * C))) / (2 * A);

    return new Scaffold(
        appBar: new AppBar(
          title: new Text('second degree equation'),
          backgroundColor: Colors.black,
        ),
        body: new Column(
            mainAxisAlignment: MainAxisAlignment.start,
            mainAxisSize: MainAxisSize.max,
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: <Widget>[
              Form(
                  key: formstate,
                  child: Column(children: [
                    new TextFormField(
                      controller: x2,
                      style: new TextStyle(
                          fontSize: 12.0,
                          color: const Color(0xff8b0c0c),
                          fontWeight: FontWeight.w200,
                          fontFamily: "Roboto"),
                    ),
                     Text(
                      "x2+",
                      style: new TextStyle(
                          fontSize: 12.0,
                          color: const Color(0xff0e0101),
                          fontWeight: FontWeight.w200,
                          fontFamily: "Roboto"),
                    ),
                    TextFormField(
                      controller: x,
                      style: const TextStyle(
                          fontSize: 12.0,
                          color: const Color(0xffae1a1a),
                          fontWeight: FontWeight.w200,
                          fontFamily: "Roboto"),
                    ),
                    const Text(
                      "x+",
                      style: const TextStyle(
                          fontSize: 12.0,
                          color: const Color(0xff0f0000),
                          fontWeight: FontWeight.w200,
                          fontFamily: "Roboto"),
                    ),
                    new TextFormField(
                      controller: c,
                      style: new TextStyle(
                          fontSize: 12.0,
                          color: const Color(0xff970c0c),
                          fontWeight: FontWeight.w200,
                          fontFamily: "Roboto"),
                    ),
                  ])),
              Text(
                _x1.toString(),
                style: const TextStyle(
                    fontSize: 12.0,
                    color: const Color(0xff220101),
                    fontWeight: FontWeight.w200,
                    fontFamily: "Roboto"),
              ),
               Text(
                _x2.toString(),
                style: const TextStyle(
                    fontSize: 12.0,
                    color: const Color(0xff350202),
                    fontWeight: FontWeight.w200,
                    fontFamily: "Roboto"),
              )
            ]));
  }
}

你能帮我吗

9nvpjoqh

9nvpjoqh1#

将double.parse替换为double.tryParse

var A = double.tryParse(a);
var B = double.tryParse(b);
var C = double.tryParse(_c);
var _x1 = ((-1 * (A??0.0)) + sqrt(((B??0.0) * (B??0.0)) - (4 * (A??0.0) * (C??0.0)))) / (2 * (A??0.0));
var _x2 = ((-1 * (A??0.0)) - sqrt(((B??0.0) * (B??0.0)) - (4 * (A??0.0) * (C??0.0)))) / (2 * (A??0.0));
9gm1akwq

9gm1akwq2#

1.所提供代码的问题在于,_x1和_x2的值的计算是在build方法内部完成的,这意味着每次构建小部件时都会调用它,即使用户尚未输入任何值。这可能会导致错误或意外行为。
1.所以当程序运行时,你正在计算x1和x2,但是a,b和c没有输入,所以null被转换为double(这是错误)。
1.为了解决这个问题,_x1和_x2的计算应该移动到一个单独的函数中,只有当用户输入了所有必需的值时才调用该函数。
1.我做了一个提交按钮,它也会检查是否输入了所有的值,然后计算x1和x2的值,这是我在一个状态下设置的。
1.你的公式也是错的
import 'dart:math';import 'package:flutter/material. dart';

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  GlobalKey<FormState> formstate = GlobalKey();
  TextEditingController x2 = TextEditingController();
  TextEditingController x = TextEditingController();
  TextEditingController c = TextEditingController();
  bool showAnswer = false;
  var _x1;
  var _x2;
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('second degree equation'),
          backgroundColor: Colors.black,
        ),
        body: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            mainAxisSize: MainAxisSize.max,
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: <Widget>[
              Form(
                  key: formstate,
                  child: Column(children: [
                    TextFormField(
                      controller: x2,
                      style: TextStyle(
                          fontSize: 12.0,
                          color: const Color(0xff8b0c0c),
                          fontWeight: FontWeight.w200,
                          fontFamily: "Roboto"),
                      validator: (value) {
                        if (value!.isEmpty) {
                          return 'Please enter a value';
                        }
                        if (double.tryParse(value!) == null) {
                          return 'Please enter a valid number';
                        }
                        return null;
                      },
                    ),
                    Text(
                      "x2+",
                      style: TextStyle(
                          fontSize: 12.0,
                          color: const Color(0xff0e0101),
                          fontWeight: FontWeight.w200,
                          fontFamily: "Roboto"),
                    ),
                    TextFormField(
                      controller: x,
                      style: const TextStyle(
                          fontSize: 12.0,
                          color: const Color(0xffae1a1a),
                          fontWeight: FontWeight.w200,
                          fontFamily: "Roboto"),
                      validator: (value) {
                        if (value!.isEmpty) {
                          return 'Please enter a value';
                        }
                        if (double.tryParse(value!) == null) {
                          return 'Please enter a valid number';
                        }
                        return null;
                      },
                    ),
                    const Text(
                      "x+",
                      style: const TextStyle(
                          fontSize: 12.0,
                          color: const Color(0xff0f0000),
                          fontWeight: FontWeight.w200,
                          fontFamily: "Roboto"),
                    ),
                    TextFormField(
                      controller: c,
                      style: TextStyle(
                          fontSize: 12.0,
                          color: const Color(0xff970c0c),
                          fontWeight: FontWeight.w200,
                          fontFamily: "Roboto"),
                      validator: (value) {
                        if (value!.isEmpty) {
                          return 'Please enter a value';
                        }
                        if (double.tryParse(value!) == null) {
                          return 'Please enter a valid number';
                        }
                        return null;
                      },
                    ),
                  ])),
              ElevatedButton(
                child: Text("submit"),
                onPressed: () {
                  if (formstate.currentState!.validate()) {
                    var a = double.parse(x2.text);
                    var b = double.parse(x.text);
                    var c = double.parse(this.c.text);
                    var temp_x1 = ((-1 * b) + sqrt((b * b) - (4 * a * c))) / (2 * a);
                    var temp_x2 = ((-1 * b) - sqrt((b * b) - (4 * a * c))) / (2 * a);
                     setState(() {
                       showAnswer=true;
                       _x1=temp_x1;
                       _x2=temp_x2;
                     });
                     print(_x1);
                     print(_x2);
                  }
                }
              ),
              if(showAnswer)
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Text('$_x1'+"  "+'$_x2')
                ],
              )

            ]
        ),
    );
  }

              
  
  }

5和3是x1和x2的值

相关问题