Hello guys I wrote code of program calculate second degree equation in flutter and dart programming language when I wrote the code i didn’t make errors in analyzer but error appeared in emulator in red screen says invalid double what can I do with this problem in confused from this problem and review my code ?
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"),
)
]));
}
}
can you help me
2
Answers
Replace double.parse to double.tryParse
import ‘dart:math’;
import ‘package:flutter/material.dart’;
5 and 3 are values of x1 and x2