gpt4 book ai didi

android - 如何解决flutter中的 "Only static members can be accessed in initializers"?

转载 作者:行者123 更新时间:2023-12-02 15:30:08 25 4
gpt4 key购买 nike

我正在开发一个 flutter 应用程序,我需要扫描一些条形码,因此,为此,我使用了一个名为 Barcode_scan ( https://pub.dartlang.org/packages/barcode_scan ) 的插件。因此,当我尝试从存储在步骤列表中的 RaisingButton 调用函数时,问题就出现了,因为当我在 onPressed、Android studio 上调用初始化条形码扫描仪的函数时,我需要在 Stepper 小部件中显示该按钮显示此消息“只能在初始化程序中访问静态成员”。

初始化条码扫描器的函数:

Future scan() async {
try {
String barcode = await BarcodeScanner.scan();
setState(() => this.barcode = barcode);
} on PlatformException catch (e) {
if (e.code == BarcodeScanner.CameraAccessDenied) {
setState(() {
this.barcode = 'The user did not grant the camera permission!';
});
} else {
setState(() => this.barcode = 'Unknown error: $e');
}
} on FormatException{
setState(() => this.barcode = 'null (User returned using the "back"-button before scanning anything. Result)');
} catch (e) {
setState(() => this.barcode = 'Unknown error: $e');
}}

以及步骤列表的代码

List<Step> mySteps = [
new Step(title: new Text("Scan first"),
content: new Column(
children: <Widget>[
new Text("Code"),
new Container(
padding: EdgeInsets.only(top: 20),
child: new Text("A08B",style: TextStyle(
fontSize: 30,
color: Colors.red
),
)
,),
new Container(
child: new RaisedButton(onPressed: scan ,
child: new Text("Scan"),),
)
],
))];

完整的 dart 类:

void main() => runApp(MaterialApp(
home: Ubicacion(),
));

class Ubicacion extends StatefulWidget {
@override
_UbicacionState createState() => _UbicacionState();}
class _UbicacionState extends State<Ubicacion> {

String barcode = "";
Future scan() async {
try {
String barcode = await BarcodeScanner.scan();
setState(() => this.barcode = barcode);
} on PlatformException catch (e) {
if (e.code == BarcodeScanner.CameraAccessDenied) {
setState(() {
this.barcode = 'The user did not grant the camera permission!';
});
} else {
setState(() => this.barcode = 'Unknown error: $e');
}
} on FormatException{
setState(() => this.barcode = 'null (User returned using the "back"-button before scanning anything. Result)');
} catch (e) {
setState(() => this.barcode = 'Unknown error: $e');
}
}


@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('hello'),
),
body: Container(
padding: EdgeInsets.all(32.0),
child: Center(
child: Column(
children: <Widget>[

new Container(
child: new Stepper(steps: mySteps,
currentStep: this.pasoActual,
onStepContinue: (){
setState(() {
if(pasoActual <mySteps.length -1){
pasoActual++;
}else{
pasoActual = 0;
}
});
},
onStepCancel: (){
setState(() {
if(pasoActual >0){
pasoActual--;
}else{
pasoActual = 0;
}
});
},),
)


],
),
),
),
);
}

int pasoActual = 0;
List<Step> mySteps = [
new Step(title: new Text("Escanear palet"),
content: new Column(
children: <Widget>[
new Text("Codigo"),
new Text("ID",),
new Text("PLU"),
new Container(
padding: EdgeInsets.only(top: 20),
child: new Text("A08B",style: TextStyle(
fontSize: 30,
color: Colors.red
),
)
,),
new Container(
child: new RaisedButton(onPressed: null ,
child: new Text("Escanear"),),
)
],
))
];

}

最佳答案

当您在类内部声明非静态变量时尝试直接初始化该变量时,会出现上述错误。在您的情况下,我假设它是您直接初始化的 mySteps 列表。

如果您使用的是Stateful Widget,请尝试在您的initState()方法中初始化它,或者在类构造函数中初始化它,错误将会消失。

您还可以查看this回答有关同一问题的详细解释。

关于android - 如何解决flutter中的 "Only static members can be accessed in initializers"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55615417/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com