调用方法OnPressed异常

时间:2018-11-27 22:16:04

标签: firebase flutter google-cloud-firestore

我有一个按钮,我尝试使用onPressed从另一个负责创建到Firestore的事务的.dart调用方法。然后我收到一个例外:

EXCEPTION CAUGHT BY GESTURE 
The following NoSuchMethodError was thrown while handling a gesture:
The method 'create' was called on null.
Receiver: null

我正在尝试将数据从表单传递给此方法,希望可以将数据存储在Cloud Firestore中。

我的按钮:

final createButton = Padding(
    padding: EdgeInsets.symmetric(vertical: 16.0),
    child: Material(
        borderRadius: BorderRadius.circular(30.0),
        shadowColor: Colors.lightBlueAccent.shade100,
        child: MaterialButton(
          minWidth: 200.0,
          height: 42.0,
          onPressed: () {
            this._createSanitario();
            //  Navigator.of(context).pushNamed(Login.tag);
          },
          color: Colors.lightBlueAccent,
          child: Text('Adicionar sanitário',
              style: TextStyle(color: Colors.white)),
        )));

他叫的方法:

void _createSanitario() {
    sanitarioStorage.create(identificacaoC.text, localizacaoC.text,
        tipoEntradaC.text, tipoSanitarioC.text);
  }

另一个负责向Firebase进行交易的.dart方法:

Future<Sanitario> create(String identificacao, String localizacao,
      String tipoEntrada, String tipoSanitario) async {
    final TransactionHandler createTransaction = (Transaction tx) async {
      final DocumentSnapshot newDoc =
          await tx.get(sanitarioCollection.document());
      final Sanitario newSanitario = new Sanitario(
          id: newDoc.documentID,
          identificacao: identificacao,
          localizacao: localizacao,
          tipoEntrada: tipoEntrada,
          tipoSanitario: tipoSanitario);
      final Map<String, dynamic> data = _toMap(newSanitario, {
        'created': new DateTime.now().toUtc().toIso8601String(),
      });
      await tx.set(newDoc.reference, data);

      return data;
    };

    return Firestore.instance
        .runTransaction(createTransaction)
        .then(_fromMap)
        .catchError((e) {
      print('Dart erro ao gravar: $e');
      return null;
    });
  }

任何帮助都将受到欢迎。

1 个答案:

答案 0 :(得分:0)

日志中有The method 'create' was called on null. 因此,您似乎没有初始化sanitarioStorage字段