flutter - 如何将 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'),
)
],
),
),
));
}
}
并想在上面使用firstName
,lastName
并email
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){
}
} ```
解决方案
需要在你的代码中声明 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]);
}
}
推荐阅读
- android - 更改底部导航当前索引并从另一个屏幕类更新 setState()
- python - Snowflake - Python 的连接器问题
- javascript - 打字稿模块解析
- java - 添加新实体时休眠@OneToMany异常
- pandas - 我尝试使用函数将所有 0 值替换为损益
- google-cloud-platform - ansible gcp_compute_instance - 在创建 vm 实例时附加现有的外部 IP 地址
- python - Flask,如何将所有变量传递到模板 Jinja
- unit-testing - 如何在 Roxy 中测试 MarkLogic Rest API?
- java - Java next() 和 nextLine 错误
- reactjs - 如何在状态挂钩中从获取请求中设置数据(对象数组)?