I have data coming from ESP32 in this format: number,number,number and I am trynig to print it in text in flutter but I am getting invalid double.
I really read the other answers but could not figure it out. this is my code:
String _dataParser(List<int> dataFromDevice) {
return utf8.decode(dataFromDevice);
}
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Expanded(
flex: 2,
child: StreamBuilder<List<int>>( **// I am getting the error here**
stream: stream,
builder:(BuildContext context, AsyncSnapshot<List<int>> snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const CircularProgressIndicator();
} else if (snapshot.connectionState == ConnectionState.active ||
snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasError) {
temperature = -1;
speed = -1;
pressure = -1;
flow = -1;
}
if (snapshot.hasData) {
var currentValue = _dataParser(snapshot.data);
var sensorData = currentValue.split(",");
// get the temperature
if (sensorData[0] == "nan") {
temperature = -2;
}
temperature = double.parse('$sensorData[0]');
// get the speed
if (sensorData[1] == "nan") {
speed = -2;
}
speed = double.parse('$sensorData[1]');
// get the pressure
if (sensorData[2] == "nan") {
pressure = -2;
}
pressure = double.parse('$sensorData[2]');
} else {
temperature = -3;
speed = -3;
pressure = -3;
flow = -3;
}
}
return Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Column(
children: [
const Text('Temperature'),
Text('$temperature'),
],
),
Column(
children: [
const Text('Speed'),
Text('$speed'),
],
),
Column(
children: [
const Text('Pressure'),
Text('$pressure'),
] ,
),
Column(
children: [
const Text('Flow'),
Text('$flow'),
],
),
],
);
}
),
),
],
);
}
the problem is with this line:
temperature = double.parse('$sensorData[0]');
the exception is:
════════ Exception caught by widgets library ═══════════════════════════════════ The following FormatException was thrown building StreamBuilder<List>(dirty, state: _StreamBuilderBaseState<List, AsyncSnapshot<List>>#67513): Invalid double [][0] The relevant error-causing widget was StreamBuilder<List> lib\componants\device_data.dart:139 When the exception was thrown, this was the stack #0 double.parse (dart:core-patch/double_patch.dart:111:28) #1 _DeviceDataState.build. lib\componants\device_data.dart:160
the value for currentValue and sensorData when the app start is as follow:
I/flutter (25023): currentValue
I/flutter (25023):
I/flutter (25023): sensorData
I/flutter (25023): [ ]
and then when the app is connected to ESP32 is as follow:
I/flutter (25023): current
I/flutter (25023): 363.00,363.00,363.00
I/flutter (25023): sensor
I/flutter (25023): [363.00, 363.00, 363.00]
so the invalid double is only in the beginning.
CodePudding user response:
Use curly brackets. Without it, it considers only sensordata as the variable and [0] as a string.
temperature = double.parse('${sensorData[0]}');
CodePudding user response:
I fixed it by checking for null like this:
if (sensorData[0].isNotEmpty && sensorData[0] != null)