Ahmad Zaklouta
Ahmad Zaklouta

Reputation: 183

How to fix invalid double in Flutter

I have data coming from ESP32 in this format: number,number,number and I am trying 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.

Upvotes: 2

Views: 15050

Answers (6)

Trung Nguyen
Trung Nguyen

Reputation: 117

Try this:

 temperature = (sensorData[0]==''||sensorData[0]==null)?(0):(double.parse('$sensorData[0]'));

Upvotes: 0

Buddhika
Buddhika

Reputation: 325

This works for me!

 value = double.tryParse(sensorData[0]) ?? 0;

Upvotes: 1

Francis Nduba Numbi
Francis Nduba Numbi

Reputation: 3050

It means that the value that sensorData[0] is returning doesn't contain a number or isn't a number. Possibly a null value, that cannot be parsed.

Upvotes: 2

orket developer
orket developer

Reputation: 21

To convert a string into a double, double.parse()

Upvotes: 0

Ahmad Zaklouta
Ahmad Zaklouta

Reputation: 183

I fixed it by checking for null like this:

if (sensorData[0].isNotEmpty && sensorData[0] != null)

Upvotes: 1

Kaushik Chandru
Kaushik Chandru

Reputation: 17762

Use curly brackets. Without it, it considers only sensordata as the variable and [0] as a string. temperature = double.parse('${sensorData[0]}');

Upvotes: 2

Related Questions