RangeError (end): Invalid value: only valid value is 0: 1

时间:2021-05-27 14:59:56

标签: flutter dart stream-builder

The following RangeError was thrown building StreamBuilder<List<int>>(dirty, state: _StreamBuilderBaseState<List<int>, AsyncSnapshot<List<int>>>#f4f4a):
RangeError (end): Invalid value: Only valid value is 0: 1

The relevant error-causing widget was: 
  StreamBuilder<List<int>> file:///F:/epicare/lib/HomeScreen.dart:827:35
When the exception was thrown, this was the stack: 
#0      RangeError.checkValidRange (dart:core/errors.dart:333:9)
#1      new ByteData.sublistView (dart:typed_data:475:22)
#2      _dataParser (package:epicare/HomeScreen.dart:930:21)
#3      _HomeScreenState.build.<anonymous closure> (package:epicare/HomeScreen.dart:845:60)
#4      StreamBuilder.build (package:flutter/src/widgets/async.dart:545:81)
...
════════════════════════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by rendering library ═════════════════════════════════════════════════════
A RenderFlex overflowed by 99945 pixels on the bottom.
The relevant error-causing widget was: 
  Column file:///F:/epicare/lib/HomeScreen.dart:824:38

这是我现在在主屏幕上收到的错误

我正在使用 esp32 从使用 Stream Builder 的传感器获取数据,但它每次在屏幕上显示此错误一段时间,然后数据保持正常。我怎样才能避免这个错误? 我来自 Esp32 的数据是 0、1、2 的虚拟数据。 0 和 1 用于校准设备,值 = 2 我想通过向护理人员发送 SMS 来触发警报,但流构建器不断给我类似上述错误。

Stream Builder 的代码:

StreamBuilder<List<int>>(
                                    stream: stream,
                                    initialData: lastValue,
                                    builder: (BuildContext context,
                                        AsyncSnapshot<List<int>> snapshot) {
                                      if (snapshot.hasError)
                                        return Text('Error: ${snapshot.error}',
                                          style: TextStyle(
                                            fontFamily: 'SF Pro Display',
                                            fontSize: 19,
                                            color: const Color(0xffffffff),
                                            fontWeight: FontWeight.w500,
                                            height: 1.4736842105263157,
                                          ),
                                        );

                                      if (snapshot.connectionState ==
                                          ConnectionState.active) {
                                        var currentValue = _dataParser(snapshot.data);
                                        var stringValue = snapshot.data.toString();
                                        print("String data $stringValue");
                                        if(currentValue == 2)
                                      {
                                        Future.microtask(() => Navigator.push(
                                          context,
                                          MaterialPageRoute(
                                            builder: (context) {
                                              return TriggeringAlert(
                                                device: widget.device,
                                              );
                                            },
                                          ),
                                        )
                                        );

                                      }
                                      
               
                                        return Text('$currentValue',
                                          style: TextStyle(
                                            fontFamily: 'SF Pro Display',
                                            fontSize: 19,
                                            color: const Color(0xffffffff),
                                            fontWeight: FontWeight.w500,
                                            height: 1.4736842105263157,
                                          ),
                                          );
                                      } else {
                                        return Text('Check the stream',
                                          style: TextStyle(
                                            fontFamily: 'SF Pro Display',
                                            fontSize: 19,
                                            color: const Color(0xffffffff),
                                            fontWeight: FontWeight.w500,
                                            height: 1.4736842105263157,
                                          ),
                                        );
                                      }
                                    },
                                  ),

Stream/BLE 代码:

// BLE
  final String SERVICE_UUID = "4fafc201-1fb5-459e-8fcc-c5c9c331914b";
  final String CHARACTERISTIC_UUID = "beb5483e-36e1-4688-b7f5-ea07361b26a8";
  //final String SERVICE_UUID = "0365a300-8d69-4066-80c7-554298a6ec5e";
  //final String CHARACTERISTIC_UUID = "cf01c075-cb75-4dea-819e-2a79dd466bcb";
  bool isReady;
  Stream<List<int>> stream;
  List<int> lastValue;

  connectToDevice() async {
    discoverServices();
  }

  discoverServices() async {

    List<BluetoothService> services = await widget.device.discoverServices();
    services.forEach((service) {
      if (service.uuid.toString() == SERVICE_UUID) {
        service.characteristics.forEach((characteristic) {
          if (characteristic.uuid.toString() == CHARACTERISTIC_UUID) {
            characteristic.setNotifyValue(!characteristic.isNotifying);
            stream = characteristic.value;
            print(stream);
            lastValue = characteristic.lastValue;
            print(lastValue);

            setState(() {
              isReady = true;
            });
          }
        });
      }
    });
  }
_dataParser(List<int> data) {
  var value =  Uint8List.fromList(data);
  print("stream.value: $value"); // stream.value: [33]
  var hr = ByteData.sublistView(value, 0, 1);
  print("Heart rate: ${hr.getUint8(0)}");
  return hr.getUint8(0);// Heart rate: 33
}

请帮我找出错误,因为我是 Flutter 新手。

0 个答案:

没有答案