【问题标题】:Flutter - The method 'call' was called on nullFlutter - 在 null 上调用了方法“调用”
【发布时间】:2020-12-03 07:54:45
【问题描述】:

我使用 Cloud Firestore 作为后端。我已经制作了一个表格,并希望将其数据存储在数据库中。但是,当我点击保存按钮时,它会抛出此错误消息:

在处理手势时引发了以下 NoSuchMethodError: 在 null 上调用了方法“call”。 接收方:空 尝试调用:call("xyz")

这是我的代码:

import 'package:flutter/material.dart';
import 'dart:collection';
import 'package:cloud_firestore/cloud_firestore.dart';

class ListPlusForm extends StatefulWidget {
  ListPlusForm({Key key}) : super(key: key);

  @override
  _ListPlusFormState createState() => _ListPlusFormState();
}

class _ListPlusFormState extends State<ListPlusForm> {
  final _formKey = GlobalKey<FormState>();

  String _itemName;
  double _progress = 0;
  DateTime _start = new DateTime.now();
  DateTime _due;
  int _id = 123;
  bool _sliderSwitch;
  bool _reminderSwitch;
  bool _prioritySwitch;

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: MediaQuery.of(context).size.width,
      height: 235,
      child: Container(
        decoration: BoxDecoration(
          shape: BoxShape.rectangle,
          borderRadius: BorderRadius.circular(20.0),
          gradient: LinearGradient(
            begin: Alignment.topLeft,
            end: Alignment.bottomRight,
            colors: [Colors.white, Colors.grey[400]],
          ),
        ),
        child: Form(
          key: _formKey,
          child: Stack(
            children: [
              Positioned(
                top: 10,
                left: 10,
                child: Container(
                  width: 200,
                  child: TextFormField(
                      style: TextStyle(
                        fontSize: 15,
                      ),
                      decoration: InputDecoration(
                        hintText: 'Name?',
                      ),
                      onSaved: (val) => {
                            setState(() => _itemName = val),
                          }),
                ),
              ),
              Positioned(
                top: 60,
                left: 10,
                child: Row(
                  children: [
                    Text('Need a slider?'),
                    Switch(
                      value: true,
                      onChanged: (value) => {
                        setState(() => _sliderSwitch),
                      },
                    ),
                    Text('Need a reminder?'),
                    Switch(
                      value: true,
                      onChanged: (value) => {
                        _reminderSwitch = value,
                      },
                    ),
                  ],
                ),
              ),
              Positioned(
                top: 90,
                left: 10,
                child: Row(
                  children: [
                    Container(
                      width: 200,
                      child: TextFormField(
                        style: TextStyle(
                          fontSize: 15,
                        ),
                        decoration: InputDecoration(
                          hintText: 'Due when?',
                        ),
                        onTap: () => {
                          showDatePicker(
                                  context: context,
                                  initialDate: _start,
                                  firstDate: _start,
                                  lastDate: DateTime(2025))
                              .then(
                            (due) {
                              setState(
                                () {
                                  _due = due;
                                },
                              );
                            },
                          ),
                        },
                      ),
                    ),
                    Text('This a priority?'),
                    Switch(
                      value: true,
                      onChanged: (value) => {
                        _prioritySwitch = value,
                      },
                    ),
                  ],
                ),
              ),
              Positioned(
                top: 140,
                left: 10,
                child: Row(
                  children: [],
                ),
              ),
              Positioned(
                  bottom: 5,
                  left: 65,
                  child: Container(
                    width: 200,
                    height: 40,
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(20.0),
                      gradient: LinearGradient(
                          begin: Alignment.topLeft,
                          end: Alignment.bottomRight,
                          colors: <Color>[
                            const Color(0xFF5761B2),
                            const Color(0xFF1FC588),
                          ]),
                    ),
                    child: MaterialButton(
                      child: Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Icon(Icons.add_circle),
                            Text('Add item'),
                          ]),
                      onPressed: () => {
                        setState(() {
                          if (_formKey.currentState.validate()) {
                            _formKey.currentState.save();
                            Map<dynamic, dynamic> listItem =
                                new HashMap<dynamic, dynamic>();
                            listItem["Description"](_itemName);
                            listItem["Due"](_due);
                            listItem["ID"](_id);
                            listItem["Priority"](_prioritySwitch);
                            listItem["Progress"](_progress);
                            listItem["Reminder"](_reminderSwitch);
                            listItem["SliderSwitch"](_sliderSwitch);
                            listItem["Start"](_start);

                            Future<void> uploadingData(
                                var _itemName,
                                var _due,
                                var _id,
                                bool _prioritySwitch,
                                var _progress,
                                bool _reminderSwitch,
                                bool _sliderSwitch) async {
                              await Firestore.instance
                                  .collection("Task Lists")
                                  .add(listItem);
                            }
                          }
                          ;
                        }),
                      },
                    ),
                  ))
            ],
          ),
        ),
      ),
    );
  }
}



提前致谢!

【问题讨论】:

    标签: firebase flutter google-cloud-firestore


    【解决方案1】:

    问题是您调用了 HashMap 的键而不是归因它

    listItem["Description"](_itemName);
    listItem["Due"](_due);
    listItem["ID"](_id);
    listItem["Priority"](_prioritySwitch);
    listItem["Progress"](_progress);
    listItem["Reminder"](_reminderSwitch);
    listItem["SliderSwitch"](_sliderSwitch);
    listItem["Start"](_start);
    

    改成

    listItem["Description"] = _itemName;
    listItem["Due"] = _due;
    listItem["ID"] = _id;
    listItem["Priority"] = _prioritySwitch;
    listItem["Progress"] = _progress;
    listItem["Reminder"] = _reminderSwitch;
    listItem["SliderSwitch"] = _sliderSwitch;
    listItem["Start"] = _start;
    

    【讨论】:

    • 您好,您的解决方案有效,但导致另一个错误。它现在说: 无法获取令牌:com.google.firebase.firestore.FirebaseFirestoreException:getToken 由于令牌更改而中止。有什么建议吗?
    • 您是否配置了您的 Firebase 应用程序?看看那个stackoverflow.com/questions/52513691/…
    • 是的,我已经配置了应用程序。 firebase-admin 模块无法使用,因为它不是受信任的环境。我能够从这个集合中读取数据。但是写入会引发此错误。如果有帮助,我可以附上阅读代码。
    • 你能看看你的firebase项目,看看你是否有权在你的收藏上写字吗?看看规则部分应该有类似“allow read, write: if true;”的内容
    猜你喜欢
    • 2020-11-06
    • 2019-09-23
    • 2020-06-04
    • 2021-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-29
    • 2020-04-27
    相关资源
    最近更新 更多