簡體   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