简体   繁体   English

Flutter - 在 null 上调用了方法“调用”

[英]Flutter - The method 'call' was called on null

I'm using Cloud Firestore as my backend.我使用 Cloud Firestore 作为我的后端。 I've made a form and want to store its data in the database.我已经制作了一个表格,并希望将其数据存储在数据库中。 However, it throws this error message when I hit the save button:但是,当我点击保存按钮时,它会抛出此错误消息:

he following NoSuchMethodError was thrown while handling a gesture: The method 'call' was called on null.在处理手势时引发了以下 NoSuchMethodError:在 null 上调用了方法“调用”。 Receiver: null Tried calling: call("xyz")接收方:null 尝试调用:call("xyz")

Here's my code:这是我的代码:

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);
                            }
                          }
                          ;
                        }),
                      },
                    ),
                  ))
            ],
          ),
        ),
      ),
    );
  }
}



Thanks in advance!提前致谢!

The problem is your calling a key of the HashMap instead of attributing it问题是您调用 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);

change it by改变它

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM