简体   繁体   English

Flutter:蓝牙设备

[英]Flutter: Bluetoothdevice

I'm working on a flutter project that receives data from a ble device, when I created the project It worked but now I copied the program to another project and there's an error that I don't know how to solve.我正在开发一个从 ble 设备接收数据的颤振项目,当我创建项目时它工作但现在我将程序复制到另一个项目并且有一个我不知道如何解决的错误。 the error is in this line BluetoothDevice _connectedDevice;错误在这一行 BluetoothDevice _connectedDevice;

where the error is Non-nullable instance field '_connectedDevice' must be initialized.其中错误是不可为空的实例字段“_connectedDevice”必须被初始化。

I am working on a similar project.我正在做一个类似的项目。 The problem I faced was the the Field device@.... was not initiated , I used "late" for variable initialization and it does not get initiated .我面临的问题是 Field device@.... 没有启动,我使用“延迟”进行变量初始化,但它没有启动。 there are 2 solutions有2个解决方案

  1. use ?利用 ? instead of late but chances are your variable will show null value而不是迟到,但您的变量可能会显示空值

2 initialize the variable by assigning it some value of same type like this 2 通过为其分配一些相同类型的值来初始化变量,如下所示

late String name;
@override
Widget build(BuildContext context) {
  return Scaffold(
        body: Text(name)
        //runtime error: 
        //LateInitializationError: Field 'name' has not been initialized.
  );
} 

becomes:

late String name;

@override
void initState() {
  name = "Flutter Campus";// the type of value you assign should be same like in this case string
  super.initState();
}
  
@override
Widget build(BuildContext context) {
  return Scaffold(
        body: Text(name)
  );
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM