未处理的异常:错误:在此Builder窗口小部件上方找不到正确的Provider <CrudModel>

时间:2020-01-08 14:26:53

标签: listview flutter dart provider flutter-alertdialog

我在使用Flutter Provider时遇到问题。有一个AlertDialog,当我按下floatButton时将激活它。我在AlertDialog中按下“保存”按钮,然后将文本添加到我的Subject模型类中。之后,应该在Provider中添加此模型类,但是它不起作用。

  @override
  Widget build(BuildContext context)
  {
    return new Scaffold(
        appBar: new AppBar(
          title: new Text('Subjects'),
        ),
      body: listView(),
      floatingActionButton: new FloatingActionButton(
        onPressed: () {
          _alertDialog(context);
        },
        child: Icon(Icons.add),
      ),
    );
  }


  _alertDialog(BuildContext context) async {
    _textEditingController.clear();
    await showDialog<String>(
        context: context,
        builder: (BuildContext context) {
          return AlertDialog(
            content: new Row(
              children: <Widget>[
                new Expanded(
                  child: TextField(
                  controller: _textEditingController,
                  style: new TextStyle(fontSize: 20.0),
                  decoration: new InputDecoration(
                    labelText: 'Add new subject',
                  ),
                ),
                )
              ],
            ),
            actions: <Widget>[
              new FlatButton(
                child: const Text('Cancel'),
                onPressed: () {
                  Navigator.of(context).pop();
                },
              ),
              new  FlatButton(
                child: const Text('Save'),
                onPressed: ()
                async {
                  var subjectProvider = Provider.of<CrudModel>(context, listen: false);
                  subject = new Subject(_textEditingController.text.toString(), "homework");
                  await subjectProvider.addSubject(subject);
                  setState(() {
                    listSub.add(subject);
                  });  
                  Navigator.of(context).pop();
                },
              )
            ],
          );
        });
  }

0 个答案:

没有答案