Forms 颤振与振动;Dart,如何在扫描和更新文本时将条形码扫描值传递到TextFormField

Forms 颤振与振动;Dart,如何在扫描和更新文本时将条形码扫描值传递到TextFormField,forms,dart,flutter,state,Forms,Dart,Flutter,State,扫描后如何将条形码值传递到TextFor字段?条形码功能将条形码值传递给字符串条形码。它是具有定义为scan()的OnTap函数的ListTile对象 我想立即将该值传递回字段。我尝试了一个setState()函数,但没有完全理解它。谢谢 import 'dart:async'; import 'package:barcode_scan/barcode_scan.dart'; import 'package:cloud_firestore/cloud_firestore.dart'; impor

扫描后如何将条形码值传递到TextFor字段?条形码功能将条形码值传递给字符串条形码。它是具有定义为scan()的OnTap函数的ListTile对象

我想立即将该值传递回字段。我尝试了一个setState()函数,但没有完全理解它。谢谢

import 'dart:async';
import 'package:barcode_scan/barcode_scan.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'ItemData.dart';
import 'homepage.dart';
import 'Barcode.dart';
import 'package:flutter/services.dart';


class CreateWidget extends StatefulWidget {
  @override
  CreateState createState() => CreateState();

}
Data newData = new Data();

class CreateState extends State<CreateWidget> {
final GlobalKey<FormState> _formKey = new GlobalKey<FormState>();
void submit() {
    _formKey.currentState.save();

}



  String barcode = "";

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Column(
    children: <Widget>[
      Flexible(
        flex: 0,
        child: Center(
          child: Form(
            key: this._formKey,
            child: Flex(
              direction: Axis.vertical,
              children: <Widget>[
                ListTile(
                  title: TextFormField (
                    initialValue: '',
                    decoration: new InputDecoration(
                      icon: new Icon(Icons.info),
                      hintText: "Title",
                    ),
                    validator: (val) => val.isEmpty ? null : 'Not a valid Title',
                    onSaved: (val) => newData.title = val,
              ),
            ),
            ListTile(
              title: TextFormField(
                initialValue: '',
                decoration: new InputDecoration(
                  icon: new Icon(Icons.info),
                  hintText: "Location",
                ),
                validator: (val) => val.isEmpty ? 'Location is required' : null,
                onSaved: (val) => newData.location = val,
              ),
            ),

对于“来自”字段的文本,创建一个控制器,在其中初始化字符串条形码,如下所示:-

TextEditingContoller c = new TextEditingController();
在列表平铺内部文本表单字段中,将其控制器设置为c,并在列表平铺的ontap函数中执行此操作

scan().then(()=>setState((){
    c.text = barcode;
}));
然后函数接受一个参数,该参数在编写代码时自动向您建议。我只在VSCode和android studio中尝试过。 因此,请确保将正确的参数传递给then函数,否则可能会出现错误。

因此,then()似乎需要声明一个类型。把绳子传进来把它修好了

scan().then((String)=>setState((){
    c.text = barcode;
}));

不工作。首先,它抛出了一个错误,它正在寻找初始值为null的值。一旦我将初始值设置为null并将c传递给then()方法,它就会扫描并尝试将文本设置为读取的条形码值,但未能将null设置为文本值。onTap:({scan()。然后((c){setState(){c.text=barcode;});},下面是错误的第一部分[VERBOSE-2:dart_error.cc(16)]未处理的异常:NoSuchMethodError:setter'text='在null上被调用。Receiver:null尝试调用:text=“9780976423317”#0 Object.noSuchMethod(dart:core/runtime/libobject_patch.dart:46:5)#1 CreateState.build(file:///Users/NewUser/Projects/FlutterTest/beacon_app/lib/entry.dart:85:23)我的错。当你在任何ide中写代码时。然后你写()函数它会自动向您显示要传递给then函数的参数。当我回答drom my mobile时,我无法看到应该传递给then()函数的参数。我希望您自己能看到。顺便说一句,我会编辑我的ans。谢谢指点。
scan().then((String)=>setState((){
    c.text = barcode;
}));