我有兩個檔案。檔案 1 有 anameController和 file2 有emailController和passwordController。我想訪問具有方法nameController的 file2 中的 from file1 。.registerUsingEmailPassword()
檔案 1
class UserName extends StatefulWidget {
const UserName({Key? key}) : super(key: key);
@override
State<UserName> createState() => _UserNameState();
}
final _registerFormKey = GlobalKey<FormState>();
final _nameTextController = TextEditingController();
...
Form(
key: _registerFormKey,
child: TextFormField(
controller: _nameTextController,
..
));
}
檔案2
class SignUp extends StatefulWidget {
const SignUp({Key? key}) : super(key: key);
@override
State<SignUp> createState() => _SignUpState();
}
class _MyEmailState extends State<SignUp> {
final _regFormKey = GlobalKey<FormState>();
final _emailController = TextEditingController();
final _passwordController = TextEditingController();
...
if (_regFormKey.currentState!.validate()) {
User? user = await FireAuth.registerUsingEmailPassword(
name: _nameTextController.text, // I want to access this controller from
file1
email: _emailTextController.text,
password:_passwordTextController.text,
);
}
}
有人可以幫忙嗎?
uj5u.com熱心網友回復:
您必須通過導航器從檔案 1轉到檔案 2 ,將nameController中的資料作為引數傳遞給導航器,例如,
Navigator.of(context).pushNamed('file2Route', arguments:nameController.text);
獲取file2中的文本為
var nameText = ModalRoute.of(context)!.settings.arguments as String;
然后只需在registerUsingEmailPassword()方法中使用 nameText 。
如果不是這種情況,請指定。
干杯!!
uj5u.com熱心網友回復:
class SignUp extends StatefulWidget {
final TextEditingController nameController; // pass via constructor
const SignUp({Key? key,required TextEditingController nameController}) : super(key: key);
@override
State<SignUp> createState() => _SignUpState();
}
然后像這樣在構建中使用
if (_regFormKey.currentState!.validate()) {
User? user = await FireAuth.registerUsingEmailPassword(
name: widget.nameController.text, // use like this
email: _emailTextController.text,
password:_passwordTextController.text,
);
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/464471.html
