我對 Dart 非常陌生,并且一般都在編碼。在觀看 YouTube 上的教程后,我生成了此代碼。在大多數情況下,我已經能夠自己解決大部分問題,但我無法弄清楚我最近的錯誤。在這里,我在一個頁面上制作了一個 TextEditingController(順便說一句效果很好),后來我轉移到另一個頁面以使代碼干凈。但他們停止了作業。
import 'package:care_ls/model/userProfileModel.dart';
import 'package:care_ls/services/database.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:care_ls/views/views.dart';
import 'package:care_ls/controllers/controllers.dart';
class ProfilePage extends StatelessWidget {
const ProfilePage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Column(
children: [
TextFormField(
controller: profilePageTextControllers().fnameController,
onChanged: (e)=> print(e),
obscureText: false,
decoration: const InputDecoration(
hintText: 'First Name',
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Color(0xff2AA8A1),
width: 1,
),
borderRadius: BorderRadius.only(
topLeft: Radius.circular(4.0),
topRight: Radius.circular(4.0),
),
),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Color(0xff272361),
width: 1,
),
borderRadius: BorderRadius.only(
topLeft: Radius.circular(4.0),
topRight: Radius.circular(4.0),
),
),
),
//style: FlutterFlowTheme.of(context).bodyText1,
),
TextFormField(
controller: profilePageTextControllers().lnameController,
obscureText: false,
decoration: const InputDecoration(
hintText: 'Second Name',
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Color(0xff2AA8A1),
width: 1,
),
borderRadius: BorderRadius.only(
topLeft: Radius.circular(4.0),
topRight: Radius.circular(4.0),
),
),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Color(0xff272361),
width: 1,
),
borderRadius: BorderRadius.only(
topLeft: Radius.circular(4.0),
topRight: Radius.circular(4.0),
),
),
),
//style: FlutterFlowTheme.of(context).bodyText1,
),
ElevatedButton(
onPressed: () async {
final userprofile = UserProfileModel(
firstName: profilePageTextControllers().fnameController.text.trim(),
lastName: profilePageTextControllers().lnameController.text.trim(),
);
await Database.addProfile(userprofile);
profilePageTextControllers().fnameController.clear();
profilePageTextControllers().lnameController.clear();
print('Data Sent');
},
child: Text('Done')),
ElevatedButton(
onPressed: () => Get.to(HomePage()), child: Text('Back')),
],
),
),
);
}
}
這是我移動到另一個檔案的控制器
import 'package:flutter/material.dart';
class profilePageTextControllers {
TextEditingController fnameController = TextEditingController();
TextEditingController lnameController = TextEditingController();
}
誰能幫助我理解我做錯了什么?
uj5u.com熱心網友回復:
你在這里犯了一個愚蠢的錯誤。當您使用時,TextFormField您應該使用全域表單鍵來處理表單。試試這個代碼片段
class ProfilePage extends StatefulWidget {
ProfilePage({Key? key}) : super(key: key);
@override
State<ProfilePage> createState() => _ProfilePageState();
}
class _ProfilePageState extends State<ProfilePage> {
final _key = GlobalKey<FormState>();
TextEditingController fnameController = TextEditingController();
TextEditingController lnameController = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Form(
key: _key,
child: Column(
children: [
TextFormField(
controller: fnameController,
onChanged: (e)=> print(e),
obscureText: false,
decoration: const InputDecoration(
hintText: 'First Name',
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Color(0xff2AA8A1),
width: 1,
),
borderRadius: BorderRadius.only(
topLeft: Radius.circular(4.0),
topRight: Radius.circular(4.0),
),
),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Color(0xff272361),
width: 1,
),
borderRadius: BorderRadius.only(
topLeft: Radius.circular(4.0),
topRight: Radius.circular(4.0),
),
),
),
//style: FlutterFlowTheme.of(context).bodyText1,
),
TextFormField(
controller: lnameController,
obscureText: false,
decoration: const InputDecoration(
hintText: 'Second Name',
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Color(0xff2AA8A1),
width: 1,
),
borderRadius: BorderRadius.only(
topLeft: Radius.circular(4.0),
topRight: Radius.circular(4.0),
),
),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Color(0xff272361),
width: 1,
),
borderRadius: BorderRadius.only(
topLeft: Radius.circular(4.0),
topRight: Radius.circular(4.0),
),
),
),
//style: FlutterFlowTheme.of(context).bodyText1,
),
ElevatedButton(
onPressed: () async {
final userprofile = UserProfileModel(
firstName: fnameController.text.trim(),
lastName: lnameController.text.trim(),
);
await Database.addProfile(userprofile);
fnameController.clear();
lnameController.clear();
print('Data Sent');
},
child: Text('Done')),
ElevatedButton(
onPressed: () => Get.to(HomePage()), child: Text('Back')),
],
),
),
),
);
}
}
uj5u.com熱心網友回復:
您的問題來源是您沒有state正確了解,然后您嘗試使用狀態管理構建專案。
首先,您需要了解Stateless 與 Stateful 小部件。然后,你需要學習狀態管理
不要get在代碼的每個角落都使用該包,從而在精神上屈服于該包。
現在,讓我們剖析您的代碼。
當您使用以下代碼時:
controller: profilePageTextControllers().fnameController,
您正在創建一個新profilePageTextControllers物件,然后訪問它的fnameController屬性。
然后你的其他代碼:
controller: profilePageTextControllers().lnameController,
正在創建另一個profilePageTextControllers物件。
所以,你現在有 2 個不同的物件。
onPressed然后在按鈕中創建另一個物件:
final userprofile = UserProfileModel(
firstName: profilePageTextControllers().fnameController.text.trim(),
lastName: profilePageTextControllers().lnameController.text.trim(),
);
fnameController因此,您正在嘗試lnameController從TextFormField.
你可以通過創建一個profilePageTextControllers變數來修復你的代碼邏輯,然后在你的TextFormField. 像這樣的東西:
class ProfilePage extends StatelessWidget {
const ProfilePage({Key? key}) : super(key: key);
var _controllers = profilePageTextControllers();
@override
Widget build(BuildContext context) {
return Scaffold(
body : SafeArea(
child: Column(
children: [
TextFormField(
controller: _controllers.fnameController,
...
),
TextFormField(
controller: _controllers.lnameController,
...
),
// Use _controllers in button onPressed too.
]
)
)
);
}
}
Or for the better solution, you can follow @saiful-islam answer by using StatefulWidget.
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/439624.html
