skip to Main Content

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

error

2

Answers


  1. Replace double.parse to 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));
    
    Login or Signup to reply.
    1. The problem with the provided code is that the calculation of the values of _x1 and _x2 is done inside the build method, which means that it is called every time the widget is built, even if the user has not yet entered any values. This can cause errors or unexpected behavior.
    2. So when program runs you are computing x1 and x2 but a,b and c are not entered so null is getting converted to a double (that is the error).
    3. To fix this, the calculation of _x1 and _x2 should be moved to a separate function that is only called when the user has entered all the required values.
    4. I had made a submit button which will also check if all values are entered or not, and then compute value of x1 and x2 , which i am setting in a state.
    5. your formula is also wrong

    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')
                    ],
                  )
    
                ]
            ),
        );
      }
    
                  
      
      }
    

    enter image description here
    5 and 3 are values of x1 and x2

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search