繁体   English   中英

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

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

我在 flutter 中非常新。 我不知道该怎么做才能解决这个问题。

我正在尝试使用 Flutter 插件: flutter_numpad_widget

这是我的完整代码:

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

我在这里关注文档: onInputValidChange

但在这一行中,它不断让我出现错误“无法在初始化程序中访问实例成员'setState'。”:

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

我已经在几天内搜索并一无所获。

感谢您的帮助

您应该在 state 小部件中声明您的 state,如下所示:

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

为您的问题添加一些解释,我认为通常也是有效的:

您应该在initState中初始化所有 state 属性。 如果你有喜欢的布尔标志或原始属性,但对象,一般来说,你应该在```initState```中初始化。 在你的情况下:

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.

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