'package:flutter/src/widgets/navigator.dart':断言失败:第 4517 行 pos 12:'!_debugLocked':不是真的

时间:2021-05-26 13:33:52

标签: flutter dart bluetooth-lowenergy

我正在使用 BLE esp32 从传感器获取数据,但是当我想通过从 BLE 设备获取数据进行校准时,它在我在设备设置屏幕中使用的 Stream Builder 上出现此错误。我一按配置,它就会移动到设备设置页面。我想要的是,在我从 BLE 获得值 1 之前,它应该继续显示此屏幕

device set up page

一旦 1 出现,它应该转到另一个屏幕显示一条消息和图片,然后在 2 秒后自动移动到主页。

device set up done page

但它显示底部溢出错误消息“蓝牙未激活”,我该如何避免?并跳转到下一页? BLE 也处于活动状态,因为我检查了另一个屏幕上的值,但是对于连接的设备,此屏幕上没有发生这种情况。此外,设备完成页面甚至不会进入主屏幕 我是 FLutter 的新手,这就是为什么我不知道我应该用它做什么。

设备设置屏幕代码:

import 'dart:typed_data';

import 'package:epicare/ConfDeviceDone.dart';
import 'package:epicare/ConfigurationScreen.dart';
import 'package:flutter/material.dart';
import 'package:flutter_blue/flutter_blue.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:fluttertoast/fluttertoast.dart';

import 'Homepage.dart';

class ConfDeviceSet extends StatefulWidget {
  const ConfDeviceSet({Key key, this.device}) : super(key: key);
  final BluetoothDevice device;
  @override
  _ConfDeviceSetState createState() => _ConfDeviceSetState();
}

class _ConfDeviceSetState extends State<ConfDeviceSet> {
  // BLE
  final String SERVICE_UUID = "4fafc201-1fb5-459e-8fcc-c5c9c331914b";
  //final String SERVICE_UUID = "0365a300-8d69-4066-80c7-554298a6ec5e";
  //final String CHARACTERISTIC_UUID = "cf01c075-cb75-4dea-819e-2a79dd466bcb";
  final String CHARACTERISTIC_UUID = "beb5483e-36e1-4688-b7f5-ea07361b26a8";
  bool isReady;
  Stream<List<int>> stream;
  List<int> lastValue;
  List<double> traceDust = List();
  @override
  void initState() {
    isReady = false;
    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
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        backgroundColor: const Color(0xffE5E0A1),
        elevation: 0,
        centerTitle: true,
        title: Text(
          "Configuration",
          style: TextStyle(
            fontSize: 15.0,
            color: Colors.black,
            fontFamily: 'Montserrat',
            fontWeight: FontWeight.normal,
          ),
        ),
        leading: IconButton(
          icon: Icon(
            Icons.arrow_back,
            color: Colors.black,
          ),
          onPressed: () {
            Navigator.push(
              context,
              MaterialPageRoute(
                builder: (context) {
                  return ConfigurationScreen();
                },
              ),
            );
          },
        ),
      ),
      body: Column(
        children: [
          Container(
            padding: EdgeInsets.only(top: 65),
            alignment: Alignment.center,
            child: Text(
              'Device setting up...',
              style: TextStyle(
                fontFamily: 'Montserrat',
                fontWeight: FontWeight.w600,
                fontSize: 17,
                color: const Color(0xa8000000),
                height: 1.3333333333333333,
              ),
              textHeightBehavior:
                  TextHeightBehavior(applyHeightToFirstAscent: false),
              textAlign: TextAlign.center,
            ),
          ),
          Container(
            padding: EdgeInsets.symmetric(vertical: 120),
            child: const SpinKitFadingCircle(
              color: const Color(0xffF1C40E),
              size: 200,
            ),
          ),
          StreamBuilder<List<int>>(
            stream: stream,
            initialData: lastValue,
            builder: (BuildContext context, AsyncSnapshot<List<int>> snapshot) {
              var currentValue = _dataParser(snapshot.data);
              if (currentValue == 1) {
                print("Test Conf 1");
                Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (context) {
                      return ConfDeviceDone(
                        device: widget.device,
                      );
                    },
                  ),
                );
              }
            },
          ),
        ],
      ),
    );
  }
}

设备完成屏幕代码

import 'package:epicare/Homepage.dart';
import 'package:flutter/material.dart';
import 'package:flutter_blue/flutter_blue.dart';

import 'ConfigurationScreen.dart';

class ConfDeviceDone extends StatefulWidget {
  const ConfDeviceDone({Key key, this.device}) : super(key: key);
  final BluetoothDevice device;
  @override
  _ConfDeviceDoneState createState() => _ConfDeviceDoneState();
}

class _ConfDeviceDoneState extends State<ConfDeviceDone> {
  @override
  void initState() {
    // isReady = false;
    // discoverServices();
    var d = Duration(seconds: 2);
    // delayed 5 seconds to next page
    Future.delayed(d, () {
      // to next page and close this page
      Navigator.pushAndRemoveUntil(
        context,
        MaterialPageRoute(
          builder: (context) {
            return Homepage(device: widget.device);
          },
        ),
            (route) => false,
      );
    });

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        backgroundColor: const Color(0xffE5E0A1),
        elevation: 0,
        centerTitle: true,
        title: Text(
          "Configuration",
          style: TextStyle(
            fontSize: 15.0,
            color: Colors.black,
            fontFamily: 'Montserrat',
            fontWeight: FontWeight.normal,
          ),
        ),
      ),
      body: Column(
        children: [
          Container(
            padding: EdgeInsets.only(top: 65),
            alignment: Alignment.center,
            child: Text(
              'Device Configured Successfully!',
              style: TextStyle(
                fontFamily: 'Montserrat',
                fontWeight: FontWeight.w600,
                fontSize: 15,
                color: const Color(0xa8000000),
                height: 1.3333333333333333,
              ),
              textHeightBehavior:
                  TextHeightBehavior(applyHeightToFirstAscent: false),
              textAlign: TextAlign.center,
            ),
          ),
          Container(
            padding: EdgeInsets.symmetric(vertical: 120),
            child: Image.asset('assets/images/green-tick.gif'),
          ),
        ],
      ),
    );
  }
}

0 个答案:

没有答案