Flutter注册报错:type 'TextEditingController' is not a subtype of type 'String'
解决Flutter注册功能中的
type 'TextEditingController' is not a subtype of type 'String'错误 你遇到的错误源于调用认证方法时,直接传递了TextEditingController对象,而方法要求的是String类型的输入值,导致类型不匹配。
问题代码定位
在注册按钮的onPress回调中,调用signupMethod和storeUserData时,错误地传入了控制器本身而非输入框的文本内容:
// 错误写法:传递控制器对象 await controller.signupMethod( context: context, email: emailController, password: passwordController, ) // 错误写法:传递控制器对象 controller.storeUserData( name: nameController, email: emailController, password: passwordController )
修改后的代码
将所有传递控制器的地方改为获取控制器的.text属性(即输入框的实际字符串内容):
ourButton( title: signup, color: redColor, textColor: whiteColor, onPress: () async { if (passwordController.text == confirmPasswordController.text) { if (isCheck != false) { try { await controller .signupMethod( context: context, email: emailController.text, // 获取输入框文本 password: passwordController.text, // 获取输入框文本 ) .then((value) { return controller .storeUserData( name: nameController.text, // 获取输入框文本 email: emailController.text, // 获取输入框文本 password: passwordController.text) // 获取输入框文本 .then((value) { VxToast.show(context, msg: 'Account Created Sucessfully'); Get.offAll(LoginScreen()); }); }); } catch (e) { VxToast.show(context, msg: e.toString()); FirebaseAuth.instance.signOut(); } } } else { VxToast.show(context, msg: 'Confirm password should be same'); } }).box.width(context.screenWidth - 50).make(),
核心修改点
- 调用
signupMethod时,将email: emailController改为email: emailController.text,password: passwordController改为password: passwordController.text - 调用
storeUserData时,将name: nameController改为name: nameController.text,email: emailController改为email: emailController.text,password: passwordController改为password: passwordController.text
修改后即可正确传递输入框的文本内容(String类型)给对应方法,解决类型不匹配的错误。
内容的提问来源于stack exchange,提问作者Husnain Khurshid
相关产品推荐
相关产品推荐

