0
votes

I have a problem with that situation. Can you help me ? I'm taking this error message.

Exception has occurred. I want save a thing in a list but show:

The following NoSuchMethodError was thrown while handling a gesture: The method 'salvar' was called on null. Receiver: null Tried calling: salvar(Instance of 'Tarefa')

THE CODE:

class TarefaScreen extends StatefulWidget {
  @override
  _TarefaScreenState createState() => _TarefaScreenState();
}

class _TarefaScreenState extends State<TarefaScreen> {
  final _formKey = GlobalKey<FormState>();
  TarefaService _tarefaService;

   String _titulo;
   String _descricao;
   DateTime _dataHora;

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

  }

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

  _save() {
    if (_formKey.currentState.validate()) {
      _formKey.currentState.save();

      Tarefa _tarefa = Tarefa(
          titulo: this._titulo,
          descricao: this._descricao,
          dataHora: this._dataHora);

      this._tarefaService.salvar(_tarefa).then((value) {
        showInfo("Tarefa adicionada");
        Navigator.of(context).pop();
      }
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Adicionar Tarefa")
        ,
      ),
    body: Form(
      key: _formKey,
      child: ListView(
        shrinkWrap: true,
        children: <Widget>[...

          Padding(
            padding: const EdgeInsets.only(top: 30, left: 10, right: 10, bottom: 5),
            child: RaisedButton(
              child: Text("Enviar"),
              onPressed: () {
                this._save();
              },
            ),
          ),
        ],
      ),
    ),
    );
  }
}

And I define addItem here;

class TarefaService {

  final TarefaStore tarefaStore;

  TarefaService(this.tarefaStore);

  Future<List<Tarefa>> buscarTarefas() {
    return Future.value(tarefaStore.tarefas);
  }

  Future<Tarefa> salvar(Tarefa atividade){
    tarefaStore.adicionarTarefa(atividade);
    return Future.value(atividade);
  }

  void dispose(){

  }
}

Please, help me

1

1 Answers

0
votes

You are trying to call the method on the object which is not instantiated so its null in this._tarefaService.salvar(_tarefa). You need to instantiate _tarefaService. You can do it in init()

_tarefaService = new TarefaService(tarefaStore);