when i want to run the project
its giving me an error for the double telling me it is not double
main :
import 'package:flutter/material.dart';
import 'homepage.dart';
void main(List<String> args) {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const Homepage();
}
}
import 'package:flutter/material.dart';
class Homepage extends StatelessWidget {
const Homepage({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
backgroundColor: const Color.fromARGB(77, 62, 60, 60),
appBar: AppBar(
elevation: 0,
backgroundColor: const Color.fromARGB(0, 35, 34, 34),
title: const Center(
child: Text(
'BMI Calculator',
style: TextStyle(color: Colors.amber),
),
),
),
body: const MyTextFormField(),
),
);
}
}
class MyTextFormField extends StatefulWidget {
const MyTextFormField({super.key});
@override
State<MyTextFormField> createState() => _MyTextFormFieldState();
}
class _MyTextFormFieldState extends State<MyTextFormField> {
final GlobalKey _formkey = GlobalKey<FormState>();
final TextEditingController _controllerone = TextEditingController();
final TextEditingController _controllertwo = TextEditingController();
String resulttext = '';
double result = 0;
@override
Widget build(BuildContext context) {
return Center(
child: SingleChildScrollView(
child: Column(
children: [
const SizedBox(
height: 30,
),
Form(
key: _formkey,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(
height: 50,
width: 100,
child: TextFormField(
keyboardType: TextInputType.number,
onChanged: (value) {
_controllerone.text = value;
},
controller: _controllerone,
decoration: const InputDecoration(
labelText: 'Height',
labelStyle:
TextStyle(fontSize: 30, color: Colors.white),
),
),
),
const SizedBox(
width: 30,
),
SizedBox(
height: 50,
width: 100,
child: TextFormField(
keyboardType: TextInputType.number,
onChanged: (value) {
_controllertwo.text = value;
},
controller: _controllertwo,
decoration: const InputDecoration(
label: Text(
'Weight',
style: TextStyle(fontSize: 30, color: Colors.white),
),
),
),
),
],
),
),
const SizedBox(
height: 40,
),
TextButton(
onPressed: calcuator(),
child: const Text(
'Calculate',
style: TextStyle(
color: Colors.amber,
fontSize: 25,
fontWeight: FontWeight.bold,
),
),
),
const SizedBox(
height: 60,
),
Text(
'$result',
style: const TextStyle(
color: Colors.white,
fontSize: 50,
fontWeight: FontWeight.bold,
),
),
const SizedBox(
height: 10,
),
Text(
resulttext,
style: const TextStyle(
color: Colors.white,
fontSize: 50,
fontWeight: FontWeight.bold,
),
),
Column(
children: [
const SizedBox(
height: 115,
),
Align(
alignment: Alignment.centerRight,
child: ContainerM.containerM(
heightbox: 25,
widthbox: 100,
rightnum: 0,
leftnum: 50,
),
),
const SizedBox(
height: 42,
),
Align(
alignment: Alignment.centerRight,
child: ContainerM.containerM(
heightbox: 25,
widthbox: 45,
rightnum: 0,
leftnum: 50,
),
),
const SizedBox(
height: 42,
),
Align(
alignment: Alignment.centerRight,
child: ContainerM.containerM(
heightbox: 25,
widthbox: 115,
rightnum: 0,
leftnum: 50,
),
),
const SizedBox(
height: 42,
),
Align(
alignment: Alignment.centerLeft,
child: ContainerM.containerM(
heightbox: 25,
widthbox: 90,
rightnum: 50,
leftnum: 0,
),
),
const SizedBox(
height: 42,
),
Align(
alignment: Alignment.centerLeft,
child: ContainerM.containerM(
heightbox: 25,
widthbox: 60,
rightnum: 50,
leftnum: 0,
),
),
],
)
],
),
),
);
}
calcuator() {
double heightnum = 0;
double weightnum = 0;
setState(() {
if (_controllerone != null && _controllertwo != null) {
heightnum = double.parse(_controllerone.text); **************here giving me the error***
weightnum = double.parse(_controllertwo.text);
}
if (heightnum <= 3) {
heightnum = double.parse(_controllerone.text);
} else if (heightnum > 4) {
heightnum = double.parse(_controllerone.text) / 100;
}
result = weightnum / (heightnum * heightnum);
if (result <= 18.5) {
resulttext = 'You need to eat more';
} else if (result >= 18.5 && result <= 24.5) {
resulttext = 'You are healty';
} else if (result >= 25 && result <= 29.9) {
resulttext = 'You need to run';
} else if (result >= 30) {
resulttext = 'Damm bro you are so fat';
}
});
}
}
class ContainerM {
late bool right;
late bool left;
static containerM({
required double heightbox,
required double widthbox,
required double rightnum,
required double leftnum,
}) {
return Container(
width: widthbox,
height: heightbox,
decoration: BoxDecoration(
color: Colors.amber,
shape: BoxShape.rectangle,
borderRadius: BorderRadius.horizontal(
left: Radius.circular(leftnum),
right: Radius.circular(rightnum),
),
),
);
}
}
class Counter {
static counter({
required double height,
required double weight,
}) {
double res = weight / (height * height);
return res;
}
}
i tryed alot bot it didnt fix whyyyyyy ? help the error File : doublepatch.dart =>
@patch
static double parse(String source,
[@deprecated double one rror(String source)?]) {
var result = _parse(source);
if (result == null) {
if (onError == null) throw new FormatException("Invalid double", source);
return one rror(source);
}
return result;
}
the error in my cods : Exception has occurred. FormatException (FormatException: Invalid double )
in the * part giving me the error for the double i think its for first the flutter going to do the mathmatik thing and then it see that the controllerone is null so i add if to it for if the controller was not null contine but it didnt work pleas help me
CodePudding user response:
can you please print out _controllerone.text.runtimeType
and double.parse(_controllerone.text)
. May we can see what is going wrong. :)
In the your TextFormField you dont have to add onChange. The controller automatically detects the change and saves the value into controller.text.
CodePudding user response:
You have to update if-else condition in calcuator
func. Rather than null, check for empty string and also you have to check for text value.
Update your function as follow.
setState(() {
if (_controllerone.text != "" && _controllertwo.text != "") {
heightnum =
double.parse(_controllerone.text); //here giving me the error***
weightnum = double.parse(_controllertwo.text);
if (heightnum <= 3) {
heightnum = double.parse(_controllerone.text);
} else if (heightnum > 4) {
heightnum = double.parse(_controllerone.text) / 100;
}
result = weightnum / (heightnum * heightnum);
if (result <= 18.5) {
resulttext = 'You need to eat more';
} else if (result >= 18.5 && result <= 24.5) {
resulttext = 'You are healty';
} else if (result >= 25 && result <= 29.9) {
resulttext = 'You need to run';
} else if (result >= 30) {
resulttext = 'Damm bro you are so fat';
}
}
});