如何解决在flutter中“只能在初始化程序中访问静态成员”?

时间:2019-04-10 14:43:30

标签: android flutter

我正在开发一个flutter应用程序,需要在其中扫描一些条形码,因此,为此,我使用了一个名为barcode_scan(https://pub.dartlang.org/packages/barcode_scan)的插件。 因此,当我尝试从存储在步骤列表中的RaisedButton调用函数时会出现问题,因为当我在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"),),
        )
      ],
    ))];

全飞镖类:

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"),),
                    )
                ],
            ))
];

}

1 个答案:

答案 0 :(得分:0)

当您在类中声明非静态变量时尝试直接对其进行初始化时,会发生上述错误。 对于您的情况,我假设这是您直接初始化的mySteps列表。

如果使用initState()或在类构造函数中,请尝试在Stateful Widget方法中对其进行初始化,并且错误将消失。

您也可以查看this答案以获取有关同一问题的详细说明。