当我尝试插入sqflite数据库Flutter-moor时,如何textfield总是返回null

时间:2019-10-09 09:47:58

标签: flutter dart

我正在尝试使用TextFormField中的值插入表中,但是每当我提交所有字段的值都为null时,insert语句就无法正常工作

这是包含表单的类,并且插入语句在下面为dao.InsertIntoItemList(itemList)。它的意思是跳回到页面,它将在表格中显示插入的项目,但我最终遇到此错误

I/flutter ( 7352): Inserted new item
E/flutter ( 7352): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: InvalidDataException: Sorry, ItemList(id: null, itemName: null, noOfItems: null, unitPrice: null, checkListId: 1) cannot be used for that because:
E/flutter ( 7352): • itemName: This value was required, but isn't present
E/flutter ( 7352): • noOfItems: This value was required, but isn't present
E/flutter ( 7352): • unitPrice: This value was required, but isn't present
E/flutter ( 7352):
[38;5;248mE/flutter ( 7352): #0      VerificationContext.throwIfInvalid[39;49m
[38;5;248mE/flutter ( 7352): #1      InsertStatement._validateIntegrity[39;49m
[38;5;248mE/flutter ( 7352): #2      InsertStatement.insert[39;49m
E/flutter ( 7352): <asynchronous suspension>
[38;5;248mE/flutter ( 7352): #3      ItemListDao.insertItemList[39;49m
[38;5;248mE/flutter ( 7352): #4      _InputNewItemState.build.<anonymous closure>[39;49m
[38;5;244mE/flutter ( 7352): #5      _InkResponseState._handleTap[39;49m
[38;5;244mE/flutter ( 7352): #6      _InkResponseState.build.<anonymous closure>[39;49m
[38;5;244mE/flutter ( 7352): #7      GestureRecognizer.invokeCallback[39;49m
[38;5;244mE/flutter ( 7352): #8      TapGestureRecognizer._checkUp[39;49m
[38;5;244mE/flutter ( 7352): #9      TapGestureRecognizer.handlePrimaryPointer[39;49m
[38;5;244mE/flutter ( 7352): #10     PrimaryPointerGestureRecognizer.handleEvent[39;49m
[38;5;244mE/flutter ( 7352): #11     PointerRouter._dispatch[39;49m
[38;5;244mE/flutter ( 7352): #12     PointerRouter.route[39;49m
[38;5;244mE/flutter ( 7352): #13     GestureBinding.handleEvent[39;49m
[38;5;244mE/flutter ( 7352): #14     GestureBinding.dispatchEvent[39;49m
[38;5;244mE/flutter ( 7352): #15     GestureBinding._handlePointerEvent[39;49m
[38;5;244mE/flutter ( 7352): #16     GestureBinding._flushPointerEventQueue[39;49m
[38;5;244mE/flutter ( 7352): #17     GestureBinding._handlePointerDataPacket[39;49m
[38;5;244mE/flutter ( 7352): #18     _rootRunUnary  (dart:async/zone.dart:1136:13)[39;49m
[38;5;244mE/flutter ( 7352): #19     _CustomZone.runUnary  (dart:async/zone.dart:1029:19)[39;49m
[38;5;244mE/flutter ( 7352): #20     _CustomZone.runUnaryGuarded  (dart:async/zone.dart:931:7)[39;49m
[38;5;244mE/flutter ( 7352): #21     _invoke1  (dart:ui/hooks.dart:263:10)[39;49m
[38;5;244mE/flutter ( 7352): #22     _dispatchPointerDataPacket  (dart:ui/hooks.dart:172:5)[39;49m
E/flutter ( 7352):
I/flutter ( 7352): Moor: Sent SELECT * FROM item_lists WHERE check_list_id = ?; with args [1]
I/flutter ( 7352): Moor: Sent SELECT * FROM check_lists; with args []
I/flutter ( 7352): Moor: Sent SELECT * FROM item_lists WHERE check_list_id = ?; with args [1]

//以上是错误

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import 'itemlist.dart';

class EditItemList extends StatefulWidget {
  final List<ItemList> listOfItems;
  final String checkListName;

  const EditItemList({Key key, this.checkListName, this.listOfItems})
      : super(key: key);

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

class _EditItemListState extends State<EditItemList> {
  final key = GlobalKey<FormState>();
  var itemName;
  var noOfItems;
  var unitPrice;
  final FocusNode _itemNameFocus = FocusNode();
  final FocusNode _noOfItemsFocus = FocusNode();
  final FocusNode _unitPriceFocus = FocusNode();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.checkListName),
      ),
      body: SingleChildScrollView(
        child: Form(
          key: key,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              ListTile(
                title: TextFormField(
                  keyboardType: TextInputType.text,
                  focusNode: _itemNameFocus,
                  autofocus: true,
                  validator: (value) {
                    value == ""
                        ? value = "This field can not be empty"
                        : value = null;
                    return value;
                  },
                  onSaved: (value) {
                    itemName = value;
                  },
                  decoration: InputDecoration(
                    labelText: 'Name of item',
                    labelStyle: TextStyle(fontWeight: FontWeight.bold),
                    hintText: 'eg: apple',
                  ),
                ),
              ),
              ListTile(
                title: TextFormField(
                  keyboardType: TextInputType.number,
                  focusNode: _noOfItemsFocus,
                  autofocus: false,
                  validator: (value) {
                    value.trim().isEmpty
                        ? value = "This field can not be empty"
                        : value = null;
                    return value;
                  },
                  onSaved: (value) {
                    noOfItems = value;
                  },
                  decoration: InputDecoration(
                    labelText: 'Number of items',
                    labelStyle: TextStyle(fontWeight: FontWeight.bold),
                    hintText: 'eg: 2',
                  ),
                ),
              ),
              ListTile(
                title: TextFormField(
                  keyboardType: TextInputType.number,
                  focusNode: _unitPriceFocus,
                  autofocus: false,
                  validator: (value) {
                    value.trim().isEmpty
                        ? value = "This field can not be empty"
                        : value = null;
                    return value;
                  },
                  onSaved: (value) {
                    unitPrice = value;
                  },
                  decoration: InputDecoration(
                    labelText: 'Price per item',
                    labelStyle: TextStyle(fontWeight: FontWeight.bold),
                    hintText: 'eg: 100',
                  ),
                ),
              ),
              ListTile(
                title: RaisedButton(
                  child: Text('Add to List'),
                  onPressed: () {
                if (this.key.currentState.validate()) {
                  this.key.currentState.save();
                  itemName = itemNameTextController.text ;
                  noOfItems = noOfItemsTextController.text;
                  unitPrice = unitPriceTextController.text;
                  final dao = Provider.of<ItemListDao>(context); 
                  final itemList = ItemList(
                    id: null,
                    itemName: itemName,
                    noOfItems: noOfItems,
                    unitPrice: unitPrice,
                    checkListId: widget.checkListId,
                  );
                  dao.insertItemList(itemList);
                  debugPrint('Inserted new item');
                  Navigator.pop(context,itemList);
                }
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}

我如何处理此错误“类型'String'不是类型'int'的子类型”,我尝试将noOfitems强制转换为int并将unitPrice强制转换为int,因为它们都是IntColumn,但我仍然遇到相同的错误。请任何帮助,谢谢

0 个答案:

没有答案