如何使用代码设置TextFormField值?

时间:2018-11-07 21:00:24

标签: android flutter

在表单上,​​我有两个TextFormField,分别称为SKU和DESC。当我在SKU中放置某些内容时,我想在SQFLite数据库上对其进行验证,因此在SKU字段中放置了一个验证器。但是我不明白为什么会收到错误消息``无法将'Future'类型的值分配给'MasterItem'类型的变量。',当我调用应从数据库中提取数据的函数时,可以有人解释我在做什么错?主要是如何验证SQFlite数据库中TextFormField的值?

非常感谢您!

import 'dart:core';
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:aisscanning/data/database_helper.dart';
import 'package:aisscanning/data/inventoryitem.dart';
import 'package:aisscanning/data/masteritem.dart';

class InventoryScanning extends StatefulWidget {
  InventoryScanning({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _InventoryScanningState createState() => new _InventoryScanningState();
}

class _InventoryScanningState extends State<InventoryScanning> {
  static GlobalKey<FormState> _formKey = new GlobalKey<FormState>();
  final TextEditingController teSku = TextEditingController();
  final TextEditingController teDesc = TextEditingController();

  bool lockLoc = true;
  bool lockSubLoc = true;
  bool lockQty = true;
  bool lockPrice = true;
  bool foundItem = false;
  String itemDescription = '';
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text("Item Scanning"),
        backgroundColor: Colors.black,
      ),
      resizeToAvoidBottomPadding: false,
      body: Padding(
        padding: const EdgeInsets.only(top: 12.0),
        child: new Form(
          key: _formKey,
          autovalidate: true,
          child: new ListView(
//              padding: const EdgeInsets.symmetric(horizontal: 16.0),
              padding: new EdgeInsets.all(8.0),
              itemExtent: 60.0,
              children: <Widget>[

                new Row(
                  // SKU
                  mainAxisSize: MainAxisSize.min,
                  children: <Widget>[
                    IconButton(
                      icon: const Icon(
                        Icons.input,
                        color: const Color(0xFF167F67),
                        size: 25.0,
                      ),
                      onPressed: () {},
                    ),
                    Expanded(
                      child: TextFormField(
                        controller: teSku,
                        validator: (value) {
                          if (value.isEmpty) {
                            return '';
                          } else {
                            MasterItem item;
                            item = checkSKU(); // <==== Error
                            teDesc.text = item.desc;
                          }
                        },
                        style: new TextStyle(
                          color: const Color(0xFF0f2638),
                          fontFamily: 'ProximaNova',
                          fontStyle: FontStyle.normal,
                          fontSize: 20.0,
                        ),
                        decoration: new InputDecoration(
                          hintText: 'Enter SKU',
                          labelText: 'SKU',
                          contentPadding: new EdgeInsets.symmetric(
                              vertical: 15.0, horizontal: 10.0),
                          border: new OutlineInputBorder(
                            borderRadius: new BorderRadius.circular(25.0),
                            borderSide: new BorderSide(),
                          ),
                          labelStyle: TextStyle(
                            color: Colors.black,
                            fontWeight: FontWeight.w600,
                            fontSize: 20.0,
                          ),
                        ),
                      ),
                    ),
                    IconButton(
                      icon: const Icon(
                        Icons.search,
                        color: const Color(0xFF167F67),
                        size: 25.0,
                      ),
                      onPressed: () {},
                    ),
                  ],
                ),
                new Row(
                    // Description
                    mainAxisSize: MainAxisSize.min,
                    children: <Widget>[
                      IconButton(
                        icon: const Icon(
                          Icons.text_fields,
                          color: const Color(0xFF167F67),
                          size: 25.0,
                        ),
                        onPressed: () {},
                      ),
                      Expanded(
                        child: TextFormField(
                          controller: teDesc,
                          style: new TextStyle(
                            color: const Color(0xFF0f2638),
                            fontFamily: 'ProximaNova',
                            fontStyle: FontStyle.normal,
                            fontSize: 20.0,
                          ),
                          decoration: new InputDecoration(
                            labelText: 'Description',
                            contentPadding: new EdgeInsets.symmetric(
                                vertical: 15.0, horizontal: 10.0),
                            border: new OutlineInputBorder(
                              borderRadius: new BorderRadius.circular(25.0),
                              borderSide: new BorderSide(),
                            ),
                            labelStyle: TextStyle(
                              color: Colors.black,
                              fontWeight: FontWeight.w600,
                              fontSize: 20.0,
                            ),
                          ),
                        ),
                      ),
                    ]),

                new Row(
                  // Buttons
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: <Widget>[
                    new RaisedButton(
                      child: new Text("Save"),
                      onPressed: () {
                        addRecord();
                      },
                      shape: new RoundedRectangleBorder(
                          borderRadius: new BorderRadius.circular(30.0)),
                      color: Colors.green,
                      elevation: 4.0,
                    ),
                    new RaisedButton(
                      child: new Text("Cancel"),
                      onPressed: () {},
                      shape: new RoundedRectangleBorder(
                          borderRadius: new BorderRadius.circular(30.0)),
                      color: Colors.red,
                      elevation: 4.0,
                    ),
                  ],
                ),
              ]),
        ),
      ),
    );
  }

  Future addRecord() async {
    var db = new DatabaseHelper();
    String saveSku = teSku.text;
    String saveDesc = teDesc.text;

    print('Sku: $saveSku');
    print('Desc: $saveDesc');
    var inventoryItem = new InventoryItem(
          saveSku, saveDesc);
      await db.saveInventoryItem(inventoryItem);
  }

  Future<MasterItem> checkSKU() async {
    var db = new DatabaseHelper();
    MasterItem item;
    String checkSku = teSku.text;
    if (checkSku.trim().length > 0) {
      item = await db.getMasterDesc(checkSku);
      return item;
    }
    return item;
  }
}

0 个答案:

没有答案