我是新手,我想使用youtube教程中的代码
class _DeviceWithAvailability extends BluetoothDevice {
BluetoothDevice device;
_DeviceAvailability availability;
int? rssi;
_DeviceWithAvailability(this.device, this.availability, [this.rssi]);
}但它给了我一个错误
超类'BluetoothDevice‘没有零参数构造函数。
和建议
尝试在'BluetoothDevice‘中声明一个零参数构造函数,或者在'BluetoothDevice’中显式调用另一个构造函数。
我看了文件,但我不太清楚。
请帮我解决这个问题。
发布于 2022-01-29 11:15:21
问题在于您的BluetoothDevice设备初始化,因为构造函数需要许多强制参数,所以有两种解决方案。
1.使用如下空参数初始化此变量:
BluetoothDevice device = BluetoothDevice(...);2.添加延迟属性,在使用此var之前,需要使用正确的信息进行初始化,如下所示:
late BluetoothDevice device;然后,在使用这个var之前,您需要传递如下正确的信息:
device = BluetoothDevice(...);发布于 2022-01-29 11:47:59
你的建议意味着,
零参数构造函数实际上是一个可以用零参数调用的构造函数.这包括只接受可选参数的构造函数。
下面是一些例子:
SomeClass();//this is zero argument constructor
SomeClass2(String argument1);//this is one argument constructor
SomeClass3(String argument1,String argument2);//this is 2 argument constructorhttps://stackoverflow.com/questions/70904573
复制相似问题