大家好,我写的程序代码计算二度方程在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"),
)
]));
}
}
你能帮我吗
2条答案
按热度按时间9nvpjoqh1#
将double.parse替换为double.tryParse
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';
5和3是x1和x2的值