错误:参数类型“TextEditingController”无法分配给参数类型“String”。在 FLUTTER

Posted

技术标签:

【中文标题】错误:参数类型“TextEditingController”无法分配给参数类型“String”。在 FLUTTER【英文标题】:Error: The argument type 'TextEditingController' can't be assigned to the parameter type 'String'.in FLUTTER 【发布时间】:2021-05-13 02:32:54 【问题描述】:

我的 signup.dart 文件:

    import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';

class SignUp extends StatefulWidget 
  @override
  _SignUpState createState() => _SignUpState();


class _SignUpState extends State<SignUp> 
  final FirebaseAuth firebaseAuth = FirebaseAuth.instance;
  final _formKey = GlobalKey<FormState>();
  TextEditingController _emailTextController = TextEditingController();
  TextEditingController _passwordTextController = TextEditingController();
  TextEditingController _nameTextController = TextEditingController();
  TextEditingController _confirmPasswordTextController =
      TextEditingController();
  String gender;
  String groupValue = "Erkek";

  bool loading = false;

  @override
  Widget build(BuildContext context) 
    return Scaffold(
      body: Stack(
        children: <Widget>[
          Image.asset(
            'images/backg.jpg',
            fit: BoxFit.fill,
            width: double.infinity,
            height: double.infinity,
          ),
          Container(
            color: Colors.black.withOpacity(0.4),
            width: double.infinity,
            height: double.infinity,
          ),
          Padding(
            padding: const EdgeInsets.only(top: 200.0),
            child: Center(
              child: Form(
                  key: _formKey,
                  child: ListView(
                    children: <Widget>[
                      Padding(
                        padding:
                            const EdgeInsets.fromLTRB(14.0, 8.0, 14.0, 8.0),
                        child: Material(
                          borderRadius: BorderRadius.circular(20.0),
                          color: Colors.white.withOpacity(0.4),
                          elevation: 0.0,
                          child: Padding(
                            padding: const EdgeInsets.only(left: 12.0),
                            child: ListTile(
                              title: TextFormField(
                                controller: _passwordTextController,
                                decoration: InputDecoration(
                                  hintText: "Ad Soyad",
                                  icon: Icon(Icons.person),
                                    border: InputBorder.none
                                ),
                                // ignore: missing_return
                                validator: (value) 
                                  if (value.isEmpty) 
                                    return "İsim boşluğu doldurulmalıdır.";
                                  
                                  return null;
                                ,
                                // ignore: missing_return
                              ),
                              trailing: Icon(Icons.remove_red_eye),
                            ),
                          ),
                        ),
                      ),

                      Padding(
                        padding:
                            const EdgeInsets.fromLTRB(14.0, 8.0, 14.0, 8.0),
                        child: Material(
                          borderRadius: BorderRadius.circular(20.0),
                          color: Colors.white.withOpacity(0.4),
                          elevation: 0.0,
                          child: Padding(
                            padding: const EdgeInsets.only(left: 12.0),
                            child: TextFormField(
                                controller: _emailTextController,
                                decoration: InputDecoration(
                                  hintText: "Email",
                                  icon: Icon(Icons.email),
                                    border: InputBorder.none
                                ),
                                // ignore: missing_return
                                validator: (value) 
                                  if (value.isEmpty) 
                                    Pattern pattern =
                                        r'^(([^<>()[]\.,;:\s@"]+(.[^<>()[]\.,;:\s@"]+)*)|(".+"))@(([[0-9]1,3.[0-9]1,3.[0-9]1,3.[0-9]1,3])|(([a-zA-Z-0-9]+.)+[a-zA-Z]2,))$';
                                    RegExp regex = new RegExp(pattern);
                                    if (!regex.hasMatch(value))
                                      return "Lütfen geçerli bir mail adresi giriniz.";
                                    else
                                      return null;
                                  
                                ),
                          ),
                        ),
                      ),
                      Padding(
                        padding: const EdgeInsets.fromLTRB(14.0, 8.0, 14.0, 8.0),
                        child: new Container(
                          color: Colors.white.withOpacity(0.4),
                          child: Row(
                            children: <Widget>[
                              Expanded(
                                  child: ListTile(
                                    title: Text(
                                      "Erkek",
                                      textAlign: TextAlign.end ,
                                      style: TextStyle(color: Colors.white),
                                    ),
                                    trailing:  Radio(value: "Erkek", groupValue: groupValue, onChanged: (e)=>valueChanged(e)),
                                  )),
                              Expanded(
                                  child: ListTile(
                                    title: Text(
                                      "Kadın",
                                      textAlign: TextAlign.end ,
                                      style: TextStyle(color: Colors.white),
                                    ),
                                    trailing:  Radio(value: "Kadın", groupValue: groupValue, onChanged: (e)=>valueChanged(e)),
                                  )),
                            ],
                          ),
                        ),
                      ),
                      Padding(
                        padding:
                            const EdgeInsets.fromLTRB(14.0, 8.0, 14.0, 8.0),
                        child: Material(
                          borderRadius: BorderRadius.circular(20.0),
                          color: Colors.white.withOpacity(0.4),
                          elevation: 0.0,
                          child: Padding(
                            padding: const EdgeInsets.only(left: 12.0),
                            child: ListTile(
                              title: TextFormField(
                                controller: _passwordTextController,
                                obscureText: true,
                                decoration: InputDecoration(
                                  hintText: "Şifre",
                                  icon: Icon(Icons.lock_outline),
                                    border: InputBorder.none
                                ),
                                // ignore: missing_return
                                validator: (value) 
                                  if (value.isEmpty) 
                                    return "Şifre boşluğu doldurulmalıdır.";
                                   else if (value.length < 6) 
                                    return "Şifre 6 haneden uzun olmalı!";
                                  
                                  return null;
                                ,
                                // ignore: missing_return
                              ),
                                trailing : IconButton(icon: Icon(Icons.remove_red_eye), onPressed: ())
                            ),
                          ),
                        ),
                      ),
                      Padding(
                        padding:
                            const EdgeInsets.fromLTRB(14.0, 8.0, 14.0, 8.0),
                        child: Material(
                          borderRadius: BorderRadius.circular(20.0),
                          color: Colors.white.withOpacity(0.4),
                          elevation: 0.0,
                          child: Padding(
                            padding: const EdgeInsets.only(left: 12.0),
                            child: ListTile(
                              title : TextFormField(
                                controller: _confirmPasswordTextController,
                                obscureText: true,
                                decoration: InputDecoration(
                                  hintText: "Şifreyi Doğrula",
                                  icon: Icon(Icons.lock_outline),
                                  border: InputBorder.none
                                ),
                                // ignore: missing_return
                                validator: (value) 
                                  if (value.isEmpty) 
                                    return "Şifre boşluğu doldurulmalıdır.";
                                   else if (value.length < 6) 
                                    return "Şifre 6 haneden uzun olmalı!";
                                  else if (_passwordTextController != value)
                                    return "Şifreler uyuşmuyor.";
                                  
                                  return null;
                                ,
                                // ignore: missing_return
                              ),
                                trailing : IconButton(icon: Icon(Icons.remove_red_eye), onPressed: ()),
                            ),
                          ),
                        ),
                      ),
                      Padding(
                        padding:
                            const EdgeInsets.fromLTRB(12.0, 8.0, 12.0, 8.0),
                        child: Material(
                            borderRadius: BorderRadius.circular(20.0),
                            color: Colors.red.withOpacity(0.8),
                            elevation: 0.0,
                            child: MaterialButton(
                              onPressed: () 
                                validateForm();
                              ,
                              minWidth: MediaQuery.of(context).size.width,
                              child: Text(
                                "Kayıt Ol",
                                textAlign: TextAlign.center,
                                style: TextStyle(
                                    color: Colors.white,
                                    fontWeight: FontWeight.bold,
                                    fontSize: 15.0),
                              ),
                            )),
                      ),
                      Padding(
                          padding: const EdgeInsets.all(8.0),
                          child: InkWell(
                              onTap: () 
                                Navigator.pop(context);
                              ,
                              child: Text(
                                "Giriş Yap",
                                textAlign: TextAlign.center,
                                style: TextStyle(color: Colors.red),
                              ))),
                    ],
                  )),
            ),
          ),
          Visibility(
              visible: loading ?? true,
              child: Center(
                child: Container(
                  alignment: Alignment.center,
                  color: Colors.white.withOpacity(0.9),
                  child: CircularProgressIndicator(
                    valueColor: AlwaysStoppedAnimation<Color>(Colors.red),
                  ),
                ),
              ))
        ],
      ),
    );
  

  valueChanged(e) 
    setState(() 
      if (e == "Erkek") 
        groupValue = e;
        gender = e;
       else if (e == "Kadın") 
        groupValue = e;
        gender = e;
      
    );
  

  void validateForm() async
    FormState formState =   _formKey.currentState;
    if(formState.validate())
      User user = await firebaseAuth.currentUser;
      if(user == null)
        firebaseAuth.createUserWithEmailAndPassword(email: _emailTextController, password: _passwordTextController).then((user) => 

        );
      
    
  

我的显示错误。

正在执行热重载... 正在将文件同步到设备 sdk gphone x86... lib/pages/signup.dart:276:60:错误:参数类型“TextEditingController”不能分配给参数类型“String”。

'TextEditingController' 来自'package:flutter/src/widgets/editable_text.dart' ('/C:/src/flutter/packages/flutter/lib/src/widgets/editable_text.dart')。 firebaseAuth.createUserWithEmailAndPassword(电子邮件:_emailTextController,密码:_passwordTextController).then((用户)=> ^ lib/pages/signup.dart:276:92:错误:参数类型“TextEditingController”不能分配给参数类型“String”。

'TextEditingController' 来自'package:flutter/src/widgets/editable_text.dart' ('/C:/src/flutter/packages/flutter/lib/src/widgets/editable_text.dart')。 firebaseAuth.createUserWithEmailAndPassword(电子邮件:_emailTextController,密码:_passwordTextController).then((用户)=> ^ ==========新错误RR=================

void validateForm() 异步 FormState formState = _formKey.currentState; if (formState.validate()) 用户用户 = 等待 firebaseAuth.currentUser; 如果(用户 == 空) firebaseAuth .createUserWithEmailAndPassword( 电子邮件:_emailTextController.text, 密码:_passwordTextController.text) .then((用户) =>

           _userServices.createUser(
               
             "username": _nameTextController.text,
               "email": user.email
             

           )
     );
   
 

【问题讨论】:

【参考方案1】:

使用_emailTextController.text 而不仅仅是_emailTextController

【讨论】:

是的。有效。而且我还有另一个错误你能帮我吗?没有为“UserCredential”类型定义吸气剂“电子邮件”。 _userServices.createUser( "username": _nameTextController.text, "email": user.email 确保在类中创建了“email”字段并传递给构造函数

以上是关于错误:参数类型“TextEditingController”无法分配给参数类型“String”。在 FLUTTER的主要内容,如果未能解决你的问题,请参考以下文章

错误:参数类型“上下文”不能分配给参数类型“BuildContext”

反应类型错误“不可分配给'从不'类型的参数”

类型的参数与类型错误的参数不兼容[重复]

错误:不能将参数类型“String”分配给参数类型“Uri”。使用JsonPlaceHolder

Flutter - 错误:参数类型'String/*1*/'不能分配给参数类型'String/*2*/'

错误:参数类型“字符串?”不能分配给参数类型“字符串”