NoSuchMethodError: invalid member on null: 'save' When Trying to Save TextFormField

NoSuchMethodError: invalid member on null: 'save' When Trying to Save TextFormField

我正在尝试将 TextFormField 的内容保存到字符串中,以便将其发送到服务器,但我收到此错误

The following JSNoSuchMethodError was thrown while handling a gesture:
NoSuchMethodError: invalid member on null: 'save'

我正在使用 flutter-web。

代码

serving/POST功能尚未实现,目前仅用于测试。

class _EditViewState extends State<EditView> {
  final int cardId;
  final _formKey = GlobalKey<FormState>();
  bool _isLoading = false;
  var _card = jsonDecode('{}');
  var _changedCard = '';
  _EditViewState({this.cardId});

// <!--Snip--!>

  @override
  void initState() {
    super.initState();
    loadCard();
  }

  loadCard() async {
    setState(() {
      _isLoading = true;
    });
    _card = await read('http://localhost:8000/edit/' + cardId.toString());
    _card = jsonDecode(_card);
    _card = stripId(_card['content']);
    setState(() {
      _isLoading = false;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: const Text('Edit Card')),
        body: _isLoading
            ? CircularProgressIndicator()
            : Column(children: [
                TextFormField(
                    key: _formKey,
                    initialValue: _card,
                    maxLines: null,
                    onSaved: (value) {
                      _changedCard = value;
                    }),
                ElevatedButton(
                    child: Text('Save Changes'),
                    onPressed: () {
                      _formKey.currentState.save();
                      sendCard(_changedCard, cardId);
                      Navigator.pop(context);
                    })
              ]));
  }
}

我也尝试使用文本控制器而不是 `_formKey.currentState.save() 但出现错误:

Assertion failed
initialValue == null || controller == null
is not true

以下是我的文本控制器解决方案与之前代码的不同之处:

class _EditViewState extends State<EditView> {
// <!--Snip--!>

  final controller = TextEditingController();

// <!--Snip--!>

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

// <!--Snip--!>

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: const Text('Edit Card')),
        body: _isLoading
            ? CircularProgressIndicator()
            : Column(children: [
                TextFormField(
                    key: _formKey,
                    controller: controller,
                    initialValue: _card,
                    maxLines: null,
                    onSaved: (value) {
                      _changedCard = value;
                    }),
                ElevatedButton(
                    child: Text('Save Changes'),
                    onPressed: () {
                      // _formKey.currentState.save();
                      _changedCard = controller.text;
                      sendCard(_changedCard, cardId);
                      Navigator.pop(context);
                    })
              ]));
  }
}

我不确定我做错了什么或如何继续。

您不能使用 GlobalKey<FormState> 变量作为 TextField 的键。您必须在包装 TextField.

Form 小部件中使用它
final _formKey = GlobalKey<FormState>();

@override
Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(title: const Text('Edit Card')),
    body: _isLoading
        ? CircularProgressIndicator()
        : Form( // <-- add this widget
            key: _formKey, // <-- set the key
            child: Column(
              children: [       
                TextFormField(
                  //key: _formKey, // <-- remove this
                  controller: controller,
                  initialValue: _card,
                  maxLines: null,
                  onSaved: (value) {
                    _changedCard = value;
                  },
                ),
                ElevatedButton(
                  child: Text('Save Changes'),
                  onPressed: () {
                    if(_formKey.currentState.validate()) { // <-- this is recommended
                      _formKey.currentState.save();
                    }
                    _changedCard = controller.text;
                    sendCard(_changedCard, cardId);
                    Navigator.pop(context);
                  },
                ),
              ],
            ),
          ),
  );
}