首页 > 解决方案 > 如何将 TextEditingController 的引用变量 Form 一个类传递给另一个类

问题描述

我有一页称为registerUser.dart示例代码,给出了滚滚:

import 'package:http/http.dart' as http;
import 'package:login/URL/urls.dart';

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

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

class _UserRegisterState extends State<UserRegister> {
  GlobalKey<FormState> key = GlobalKey<FormState>();

  TextEditingController firstName = TextEditingController();
  TextEditingController LastName = TextEditingController();
  TextEditingController email = TextEditingController();

  // var listOFUrl =listOFallUrl(firstName,this.LastName,this.email);

final surj =new listOFallUrl.myUrlConstructor(firstName.text);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("Lab Work"),
        ),
        body: SingleChildScrollView(
          child: Form(
            key: key,
            child: Column(
              children: <Widget>[
                TextFormField(
                  controller: firstName,
                  decoration: InputDecoration(labelText: 'Item Name'),
                  validator: (value) =>
                  value.isEmpty ? 'this field cannot be empty' : null,
                ),
                TextFormField(
                  decoration: InputDecoration(labelText: 'Item Price'),
                  validator: (value) =>
                  value.isEmpty ? 'this field cannot be empty' : null,
                  controller: LastName,
                ),
                TextFormField(
                  controller: email,
                  decoration: InputDecoration(labelText: 'Item Description'),
                  validator: (value) =>
                  value.isEmpty ? 'this field cannot be empty' : null,
                ),
                ElevatedButton(
                  onPressed: () {
                    if (key.currentState.validate()) {
                      // putProduct();
                      print("record inserted");
                    }
                  },
                  child: Text('Save'),
                )
              ],
            ),
          ),
        ));
  }
}

并想在上面使用firstNamelastNameemail TextEditingController进入这个页面调用urls.dart。怎么可能呢?我试图通过textEditorController使用类的参考变量,listOFallUrl但没有成功。

import 'package:http/http.dart' as http;
class listOFallUrl{

  listOFallUrl(TextEditingController firtname){}

  listOFallUrl.myUrlConstructor(){
      print("this is example");
  }
  void URL(TextEditingController firtname){
        
  }
} ``` 
 

标签: flutteroopdart

解决方案


需要在你的代码中声明 List,试试下面的代码:

class UserRegister extends StatefulWidget {
  //const UserRegister({Key key}) : super(key: key);

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

class _UserRegisterState extends State<UserRegister> {
  GlobalKey<FormState> key = GlobalKey<FormState>();

  TextEditingController firstName = TextEditingController();
  TextEditingController LastName = TextEditingController();
  TextEditingController email = TextEditingController();

  putProduct(List<TextEditingController> surj){
    listOFallUrl(surj);
  }

  @override
  Widget build(BuildContext context) {
    List<TextEditingController> surj;
    return Scaffold(
        appBar: AppBar(
          title: Text("Lab Work"),
        ),
        body: SingleChildScrollView(
          child: Form(
            key: key,
            child: Column(
              children: <Widget>[
                TextFormField(
                    controller: firstName,
                    decoration: InputDecoration(labelText: 'Item Name'),
                    validator: (String? value) {
                      if (value != null && value.isEmpty) {
                        return "this field cannot be empty";
                      }
                      return null;
                    }),
                TextFormField(
                    controller: LastName,
                    decoration: InputDecoration(labelText: 'Item Price'),
                    validator: (String? value) {
                      if (value != null && value.isEmpty) {
                        return "this field cannot be empty";
                      }
                      return null;
                    }),
                TextFormField(
                    controller: email,
                    decoration: InputDecoration(labelText: 'Item Description'),
                    validator: (String? value) {
                      if (value != null && value.isEmpty) {
                        return "this field cannot be empty";
                      }
                      return null;
                    }),
                ElevatedButton(
                  onPressed: () {
                    if (key.currentState!.validate()) {
                      surj=[firstName,LastName,email];
                      putProduct(surj);
                      print("record inserted");
                    }
                  },
                  child: Text('Save'),
                )
              ],
            ),
          ),
        ));
  }
}

class listOFallUrl {
  List<TextEditingController> l;
  listOFallUrl(this.l);
  void URL(List<TextEditingController> l) {
    print(l[0]);
    print(l[1]);
    print(l[2]);
  }
}

推荐阅读