簡體   English   中英

來自 FormKey 的值在 Flutter 上始終為空

[英]Value from FormKey is always empty on Flutter

我有一個簡單的屏幕 class dart 和一個 controller 這個屏幕的形式是:

 Container(
                  height: screenHeight * 0.8,
                  width: double.infinity,
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      Image(
                        image: AssetImage('assets/images/cumpra_logo_dark.png'),
                        height: screenHeight * 0.08,
                      ),
                      SizedBox(
                        height: screenHeight * 0.04,
                      ),
                      Container(
                        width: double.infinity,
                        child: Padding(
                          padding: const EdgeInsets.all(20.0),
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: <Widget>[
                              Form(
                                key: _con.formKey,
                                child: Column(
                                  children: <Widget>[
                                    Container(
                                      width: screenWidth * 0.7,
                                      child: TextFormField(
                                        style: TextStyle(
                                          fontSize: screenHeight * 0.03,
                                          color: Colors.black,
                                        ),
                                        inputFormatters: [
                                          UpperCaseTextFormatter(),
                                        ],
                                        textAlign: TextAlign.center,
                                        decoration: InputDecoration(
                                          filled: true,
                                          fillColor: Color(0XFFe0e0e0),
                                          hintText: 'Código da Empresa',
                                          contentPadding: const EdgeInsets.only(
                                              left: 14.0,
                                              bottom: 8.0,
                                              top: 8.0),
                                          focusedBorder: OutlineInputBorder(
                                            borderSide:
                                                BorderSide(color: Colors.white),
                                            borderRadius:
                                                BorderRadius.circular(25.7),
                                          ),
                                          enabledBorder: UnderlineInputBorder(
                                            borderSide:
                                                BorderSide(color: Colors.white),
                                            borderRadius:
                                                BorderRadius.circular(25.7),
                                          ),
                                        ),
                                        validator: (value) {
                                          if (value.isEmpty) {
                                            return 'Campo obrigatório';
                                          }
                                          if (value.length < 6) {
                                            return 'Mínimo de 6 dígitos';
                                          }
                                          return null;
                                        },
                                        onChanged: (String text) {
                                          _con.userDataTemp['pin'] = text;
                                        },
                                      ),
                                    ),
                                    SizedBox(
                                      height: screenHeight * 0.04,
                                    ),
                                    Container(
                                      width: screenWidth * 0.7,
                                      child: TextFormField(
                                        keyboardType: TextInputType.number,
                                        obscureText: true,
                                        autofocus: false,
                                        style: TextStyle(
                                          fontSize: screenHeight * 0.03,
                                          color: Colors.black,
                                        ),
                                        textAlign: TextAlign.center,
                                        decoration: InputDecoration(
                                          filled: true,
                                          fillColor: Color(0XFFe0e0e0),
                                          hintText: 'Número de Acesso',
                                          contentPadding: const EdgeInsets.only(
                                              left: 14.0,
                                              bottom: 8.0,
                                              top: 8.0),
                                          focusedBorder: OutlineInputBorder(
                                            borderSide:
                                                BorderSide(color: Colors.white),
                                            borderRadius:
                                                BorderRadius.circular(25.7),
                                          ),
                                          enabledBorder: UnderlineInputBorder(
                                            borderSide:
                                                BorderSide(color: Colors.white),
                                            borderRadius:
                                                BorderRadius.circular(25.7),
                                          ),
                                        ),
                                        validator: (value) {
                                          if (value.isEmpty) {
                                            return 'Campo obrigatório';
                                          }
                                          if (value.length < 6) {
                                            return 'Mínimo de 6 dígitos';
                                          }
                                          return null;
                                        },
                                        onChanged: (String text) {
                                          _con.userDataTemp['code'] = text;
                                        },
                                      ),
                                    ),
                                    SizedBox(
                                      height: screenHeight * 0.06,
                                    ),
                                    Container(
                                      width: screenWidth * 0.7,
                                      child: RaisedButton(
                                        onPressed: _con.signIn,
                                        child: Text('LOGAR'),
                                        padding: EdgeInsets.symmetric(
                                          horizontal: 10.0,
                                          vertical: 12.0,
                                        ),
                                        shape: RoundedRectangleBorder(
                                          borderRadius:
                                              BorderRadius.circular(30.0),
                                        ),
                                        color: ThemeColor.AppSecundaryColor,
                                        textColor: Colors.white,
                                        elevation: 5,
                                      ),
                                    ),
                                    SizedBox(
                                      height: 30.0,
                                    ),
                                  ],
                                ),
                              ),
                            ],
                          ),
                        ),
                      ),
                    ],
                  ),
                )

我已經把 KEY 放在這里鍵:_con.formKey, _con 從我的 controller 參考這里是代碼:

  LoginController(context) {
    this.context = context;
    formKey = new GlobalKey<FormState>();
    this.scaffoldKey = new GlobalKey<ScaffoldState>();
    utilService = Provider.of<UtilService>(context, listen: false);
    sharedService = Provider.of<SharedService>(context, listen: false);
    userService = Provider.of<UserService>(context, listen: false);
    authService = Provider.of<AuthService>(context, listen: false);
  }

為什么總是當我按下按鈕“LOGAR”時代碼 go 來驗證:

 validator: (value) {
                                      if (value.isEmpty) {
                                        return 'Campo obrigatório';
                                      }
                                      if (value.length < 6) {
                                        return 'Mínimo de 6 dígitos';
                                      }
                                      return null;
                                    },
                                    onChanged: (String text) {
                                      _con.userDataTemp['pin'] = text;
                                    },

但總是這個值是空的,我錯過了什么?

要從 FormBuilder 中獲取值,您必須首先保存表單構建器,為此您必須考慮執行類似的操作。

Map json = formKey.currentState.saveAndValidate();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM