简体   繁体   English

无法在初始化程序中访问实例成员“setState”

[英]The instance member 'setState' can't be accessed in an initializer

im Very new in flutter.我在 flutter 中非常新。 i dont know what to do to fix this.我不知道该怎么做才能解决这个问题。

im trying to Use Flutter Plugin: flutter_numpad_widget我正在尝试使用 Flutter 插件: flutter_numpad_widget

Here my Full code:这是我的完整代码:

import 'package:flutter/material.dart';
import 'package:flutter_numpad_widget/flutter_numpad_widget.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

bool _confirmEnabled = false;

class _MyAppState extends State<MyApp> {
  int maxRawLength;
  final NumpadController _numpadController = NumpadController(
    format: NumpadFormat.NONE,
    hintText: "Ketikkan NIP",
    onInputValidChange: (bool valid) => setState(() {
      _confirmEnabled = valid;
    }),
  );
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Numpad Example',
        theme: ThemeData(
            primarySwatch: Colors.amber,
            buttonTheme: ButtonThemeData(
                textTheme: ButtonTextTheme.normal,
                buttonColor: Colors.blueGrey[300],
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.all(Radius.circular(30))))),
        home: Scaffold(
          appBar: AppBar(
            title: Text('Numpad Example'),
          ),
          body: Container(
            child: Column(
              children: <Widget>[
                Padding(
                  padding: const EdgeInsets.all(16.0),
                  child: NumpadText(
                    style: TextStyle(fontSize: 30),
                    controller: _numpadController,
                  ),
                ),
                Expanded(
                  child: Numpad(
                    controller: _numpadController,
                    buttonTextSize: 40,
                  ),
                )
              ],
            ),
          ),
        ));
  }
}

im following the documentation here: onInputValidChange我在这里关注文档: onInputValidChange

but in this line its keep getting me Error "The instance member 'setState' can't be accessed in an initializer.":但在这一行中,它不断让我出现错误“无法在初始化程序中访问实例成员'setState'。”:

onInputValidChange: (bool valid) => setState(() {
      _confirmEnabled = valid;
    }),

im Already searching in few days and gets nothing.我已经在几天内搜索并一无所获。

thanks for your help priciateit感谢您的帮助

You should declare your state inside The state widget like this:您应该在 state 小部件中声明您的 state,如下所示:

class _MyAppState extends State<MyApp> {
   int maxRawLength;
   bool _confirmEnabled = false; // here 
   ....
   onInputValidChange: (bool valid) => setState(() {
      _confirmEnabled = valid;
   }),
  ...

To add some explanation to your problem and I think in general is also valid:为您的问题添加一些解释,我认为通常也是有效的:

You should init all your state properties in initState .您应该在initState中初始化所有 state 属性。 If you have like bool flags or primitive properties that's fine but objects, in general, you should init in ```initState````.如果你有喜欢的布尔标志或原始属性,但对象,一般来说,你应该在```initState```中初始化。 In your case:在你的情况下:

import 'package:flutter/material.dart';
import 'package:flutter_numpad_widget/flutter_numpad_widget.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

bool _confirmEnabled = false;

class _MyAppState extends State<MyApp> {
  int maxRawLength;
  final NumpadController _numpadController; // this is the declaration

@override
void initState() {
  super.initState();
  _numpadController = NumpadController( // here is the init
    format: NumpadFormat.NONE,
    hintText: "Ketikkan NIP",
    onInputValidChange: (bool valid) => setState(() {
      _confirmEnabled = valid;
    }),
  );
}

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Numpad Example',
        theme: ThemeData(
            primarySwatch: Colors.amber,
            buttonTheme: ButtonThemeData(
                textTheme: ButtonTextTheme.normal,
                buttonColor: Colors.blueGrey[300],
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.all(Radius.circular(30))))),
        home: Scaffold(
          appBar: AppBar(
            title: Text('Numpad Example'),
          ),
          body: Container(
            child: Column(
              children: <Widget>[
                Padding(
                  padding: const EdgeInsets.all(16.0),
                  child: NumpadText(
                    style: TextStyle(fontSize: 30),
                    controller: _numpadController,
                  ),
                ),
                Expanded(
                  child: Numpad(
                    controller: _numpadController,
                    buttonTextSize: 40,
                  ),
                )
              ],
            ),
          ),
        ));
  }
}

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

相关问题 无法在初始化程序中访问实例成员“X”。 - Flutter,VoidCallback Function - The instance member 'X' can't be accessed in an initializer. - Flutter, VoidCallback Function Flutter :“无法使用静态访问访问实例成员“正在播放”。” - Flutter : "Instance member 'playing' can't be accessed using static access." 无法从静态方法Flutter访问实例成员 - Instance members can't be accessed from a static method Flutter 通过实例引用访问静态成员的最佳解决方案 - Best solution for static member being accessed by instance reference 通过实例引用访问的静态成员(使用“this”关键字) - Static member accessed via instance reference (using 'this' keyword) 找不到变量:setState - Can't Find Variable: setState 无法在Android Studio中调用初始化方法 - Can't call an initializer method in Android Studio 通过实例引用访问静态成员&#39;android.content.Context.MODE_PRIVATE&#39; - Static member 'android.content.Context.MODE_PRIVATE' accessed via instance reference 无法无条件访问属性“文件”,因为接收者可以为“空” - The property 'files' can't be unconditionally accessed because the receiver can be 'null' 无法无条件访问属性“窗口”,因为接收者可以为“空” - The property 'window' can't be unconditionally accessed because the receiver can be 'null'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM