【问题标题】:Why is dart HTTP post is sending previous session data?为什么 dart HTTP post 正在发送以前的会话数据?
【发布时间】:2019-07-29 22:50:52
【问题描述】:

我遇到了一个问题,即我在以前的表单保存中使用的数据作为当前行保存在数据库中。例如,在第一次迭代中,当我使用“test1”作为名字并且第二次迭代与 test2 一起保存时,第二次迭代在数据库中保存为 test1 而不是 test2。 第一次迭代保存了“名字”的空数据。

我是飞镖/颤振的新手。

class InitialRegistrationForm extends StatefulWidget {
@override
_InitialRegistrationFormState createState() =>
    _InitialRegistrationFormState();
}

class _InitialRegistrationFormState extends State<InitialRegistrationForm> {
  final _formKey = GlobalKey<FormState>();
  final _user = User();
  bool _isLoading = false;
  String msg = '';

          @override
          Widget build(BuildContext context) {
            return SingleChildScrollView(
              child: Builder(
                builder: (context) => Form(
                  key: _formKey,
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.stretch,
                    children: [
                      Container(
                        padding: const EdgeInsets.fromLTRB(0, 20, 0, 5),
                        child: Text(
                          'BASIC DETAILS',
                          style: TextStyle(
                            fontSize: 20.0,
                            color: Colors.white,
                            decoration: TextDecoration.underline,
                            decorationColor: Colors.black87,
                            decorationStyle: TextDecorationStyle.wavy,
                          ),
                        ),
                      ),
                      TextFormField(
                        decoration: InputDecoration(labelText: 'First name'),
                        style: TextStyle(
                          fontSize: 15.0,
                          color: Colors.white,
                        ),
                        validator: (value) {
                          if (value.isEmpty) {
                            return 'Please enter your first name';
                          }
                        },
                        onSaved: (val) => setState(() => _user.firstName = val),
                      ),

                      Container(
                        padding: const EdgeInsets.symmetric(
                            vertical: 16.0, horizontal: 16.0),
                        child: RaisedButton(
                          onPressed: () {
                            _irfSubmit(
                              _user.firstName,
                            );
                            final form = _formKey.currentState;
                            print(form.toStringShort());
                            if (form.validate()) {
                              form.save();
                              _user.save();
                            }
                          },
                          child: Text('Save'),
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            );
          }
          }

          _irfSubmit(String fn) async {
            SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
            try {
              final http.Response response = await http.post(
                  "http://xxxxxxxxxxxx/public_html/include/participant_form_app.php",
                  body: {
                    "firstname": fn,
                  });
                }
        }

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    如果通过保存在数据库中,您的意思是当您进行 http 调用时遇到此问题,因为您在调用 _irfSubmit(_user.firstName,); 之后调用了 form.save();。如果您切换顺序,它可能会起作用。

    【讨论】:

    • 感谢您的意见。我发现了这个问题。它的 _irfSubmit(_user.firstName, );,这应该在 form.validate if 条件内。将其移入 if (form.validate()) 后,它按预期工作。
    猜你喜欢
    • 2015-08-22
    • 1970-01-01
    • 2021-06-05
    • 2018-01-12
    • 2023-03-11
    • 2017-07-06
    • 1970-01-01
    • 2017-09-09
    • 2020-09-19
    相关资源
    最近更新 更多