Dart/Flutter error: 'context != null': is not true

Asked

Viewed 855 times

1

I’m trying to apply a dialog alert every time the item is duplicated but when I do the test it ends up giving this error, someone has some hint how to fix it ?

 void SalvarMensagem() async {
    // select id_armarios from armarios where numero_serie = '$_numeroSerie'

    this._status = _status;
    _numeroSerie = _status.substring(48, 80);

    final QuerySnapshot result = await Future.value(Firestore.instance
        .collection("lockers")
        .where("numero_serie", isEqualTo: "$_numeroSerie")
        .limit(1)
        .getDocuments());

    final List<DocumentSnapshot> documents = result.documents;
    if (documents.length == 1) {
      duplicado(context);
    } else {
      await Firestore.instance
          .collection("lockers")
          .document()
          .setData({"numero_serie": _numeroSerie});
    }
  }

duplicado(BuildContext context){
  Alert(
    context: context,
    type: AlertType.error,
    title: "QR Code já cadastrado",
    buttons: [
      DialogButton(
        child: Text(
          "OK",
          style: TextStyle(color: Colors.white, fontSize: 20),
        ),
        onPressed: () => Navigator.pop(context),
        color: Colors.lightBlueAccent,
        width: 120,
      )
    ],
  ).show();
}

Error:

E/flutter ( 9129): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: 'package:flutter/src/widgets/localizations.Dart': Failed assertion: line 446 pos 12: 'context != null': is not true.

  • Where it comes from context in SalvarMensagem?

  • comes from the duplicate function(title: "QR Code already registered")

  • The error message informs that context is null (null), I believe context must come from widget what call SalvarMensagem and then must be passed to duplicado

1 answer

1

In the save message method you are calling the duplicate function and you are passing the context by parameter, but where is the context, its definition?

Every time you need the context in some flutter code you need to 'use' it from within the main state, that method that returns the screen widget.

I know the question is old but answering here may help others.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.