Home > OS >  The argument type 'String' can't be assigned to the parameter type 'TextEditingC
The argument type 'String' can't be assigned to the parameter type 'TextEditingC

Time:01-01

The argument type 'String' can't be assigned to the parameter type 'TextEditingController?' in Flutter

Is there any way to fix this error? Please help. I looked at other posted questions but it didn't help, I even tried using the .text getter

I tried solutions from other topics, for example trying to change type of variable from String or changing 'titleController' to 'titleController.text' (the getter text isn't defined) but it didn't work.

class AddTodoPopupCard extends StatefulWidget {
  const AddTodoPopupCard({Key? key}) : super(key: key);

  @override
  State<AddTodoPopupCard> createState() => _AddTodoPopupCardState();
}

class _AddTodoPopupCardState extends State<AddTodoPopupCard> {
  /// {@macro add_todo_popup_card}
  final titleController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Padding(
        padding: const EdgeInsets.all(10.0),
        child: Hero(
          tag: heroAddTodo,
          createRectTween: (begin, end) {
            return CustomRectTween(begin: begin!, end: end!);
          },
          child: Card(
            clipBehavior: Clip.antiAliasWithSaveLayer,
            color: Colors.blueGrey,
            elevation: 2,
            shape:
                RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
            child: SizedBox(
              height: MediaQuery.of(context).size.height * 0.7,
              width: MediaQuery.of(context).size.width * 0.9,
              child: SingleChildScrollView(
                  physics: ClampingScrollPhysics(),
                  keyboardDismissBehavior:
                      ScrollViewKeyboardDismissBehavior.onDrag,
                  child: Column(
                    children: [
                      SizedBox(
                          width: MediaQuery.of(context).size.width * 0.9,
                          height: MediaQuery.of(context).size.height * 0.25,
                          child: GestureDetector(
                            onTap: () {
                              print("Add a video picker widget");
                            },
                            child: Container(
                              color: Colors.grey[600],
                              child: Column(
                                children: const [
                                  SizedBox(height: 50),
                                  Icon(
                                    Icons.video_file_rounded,
                                    size: 56,
                                  ),
                                  SizedBox(height: 5),
                                  Text(
                                    "Upload Video",
                                    style:
                                        TextStyle(fontWeight: FontWeight.bold),
                                  )
                                ],
                              ),
                            ),
                          )),
                      const Padding(
                        padding: EdgeInsets.only(
                            top: 15, bottom: 10, left: 10, right: 10),
                        child: TextField(
                          controller: titleController.text,
                          style: TextStyle(
                              fontWeight: FontWeight.bold, fontSize: 22),
                          decoration: InputDecoration(
                              hintText: 'Click to add title',
                              border: InputBorder.none,
                              counterText: ""),
                          cursorColor: Colors.white,
                          maxLines: 2,
                          maxLength: 80,
                        ),
                      ),
                      const Divider(
                        indent: 5,
                        endIndent: 5,
                        color: Colors.white,
                        thickness: 0.2,
                      ),
                      const Padding(
                        padding: EdgeInsets.only(
                            top: 15, bottom: 10, left: 10, right: 10),
                        child: TextField(
                          style: TextStyle(
                              fontWeight: FontWeight.w300, fontSize: 14),
                          decoration: InputDecoration(
                            hintText: 'Click to add description',
                            border: InputBorder.none,
                          ),
                          cursorColor: Colors.white,
                          maxLines: 14,
                          maxLength: 800,
                        ),
                      ),
                      const Divider(
                        indent: 5,
                        endIndent: 5,
                        color: Colors.white,
                        thickness: 0.2,
                      ),
                      const SizedBox(height: 30),
                      GestureDetector(
                        onTap: () {
                          print("Upload script button pressed.");
                        },
                        child: Container(
                            padding: EdgeInsets.only(left: 10),
                            margin: EdgeInsets.only(right: 115),
                            height: 50,
                            child: Card(
                              elevation: 5,
                              child: Row(
                                children: const [
                                  Icon(
                                    Icons.file_present_rounded,
                                    size: 30,
                                  ),
                                  SizedBox(width: 5),
                                  Text("Click to upload your script")
                                ],
                              ),
                            )),
                      ),
                      const SizedBox(height: 30),
                      GestureDetector(
                        onTap: () async {
                          print("object");
                        },
                        child: Container(
                          alignment: Alignment.center,
                          width: MediaQuery.of(context).size.width * 0.9,
                          height: 50,
                          color: Colors.grey[800],
                          child: Text(
                            'Submit.',
                            style: TextStyle(
                                fontSize: 28, fontWeight: FontWeight.bold),
                            textAlign: TextAlign.center,
                          )
                              .animate(
                                onPlay: (controller) =>
                                    controller.repeat(reverse: true),
                              )
                              .shimmer(
                                  color: Colors.blue,
                                  duration: Duration(seconds: 3)),
                        ),
                      )
                    ],
                  )),
            ),
          ),
        ),
      ),
    );
  }
}

CodePudding user response:

in your TextField you have to just change the controller properties and assign the controller

TextField(controller : titleController )
             

CodePudding user response:

just set correctly your controller like this

TextField( controller: titleController )

CodePudding user response:

Here

const Padding(
                        padding: EdgeInsets.only(
                            top: 15, bottom: 10, left: 10, right: 10),
                        child: TextField(
                          controller: titleController.text, // change this line of code
                          

with

Padding(
                        padding: EdgeInsets.only(
                            top: 15, bottom: 10, left: 10, right: 10),
                        child: TextField(
                          controller: titleController, // with this...
                          

titleController is an object of class TextController() and controller parameter requires object of TextController class, you can access the data inside using titleController.text, and thus cannot pass it as String in the controller parameter

CodePudding user response:

now it will work hopefully. I have removed the const keyword in 66 no line.

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

class AddTodoPopupCard extends StatefulWidget {
  const AddTodoPopupCard({Key? key}) : super(key: key);

  @override
  State<AddTodoPopupCard> createState() => _AddTodoPopupCardState();
}

class _AddTodoPopupCardState extends State<AddTodoPopupCard> {
  /// {@macro add_todo_popup_card}
  var titleController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Padding(
        padding: const EdgeInsets.all(10.0),
        child: Hero(
          tag: heroAddTodo,
          createRectTween: (begin, end) {
            return CustomRectTween(begin: begin!, end: end!);
          },
          child: Card(
            clipBehavior: Clip.antiAliasWithSaveLayer,
            color: Colors.blueGrey,
            elevation: 2,
            shape:
            RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
            child: SizedBox(
              height: MediaQuery.of(context).size.height * 0.7,
              width: MediaQuery.of(context).size.width * 0.9,
              child: SingleChildScrollView(
                  physics: ClampingScrollPhysics(),
                  keyboardDismissBehavior:
                  ScrollViewKeyboardDismissBehavior.onDrag,
                  child: Column(
                    children: [
                      SizedBox(
                          width: MediaQuery.of(context).size.width * 0.9,
                          height: MediaQuery.of(context).size.height * 0.25,
                          child: GestureDetector(
                            onTap: () {
                              print("Add a video picker widget");
                            },
                            child: Container(
                              color: Colors.grey[600],
                              child: Column(
                                children: const [
                                  SizedBox(height: 50),
                                  Icon(
                                    Icons.video_file_rounded,
                                    size: 56,
                                  ),
                                  SizedBox(height: 5),
                                  Text(
                                    "Upload Video",
                                    style:
                                    TextStyle(fontWeight: FontWeight.bold),
                                  )
                                ],
                              ),
                            ),
                          )),
                       Padding(
                        padding: EdgeInsets.only(
                            top: 15, bottom: 10, left: 10, right: 10),
                        child: TextField(
                          controller: titleController,
                          style: TextStyle(
                              fontWeight: FontWeight.bold, fontSize: 22),
                          decoration: InputDecoration(
                              hintText: 'Click to add title',
                              border: InputBorder.none,
                              counterText: ""),
                          cursorColor: Colors.white,
                          maxLines: 2,
                          maxLength: 80,
                        ),
                      ),
                      const Divider(
                        indent: 5,
                        endIndent: 5,
                        color: Colors.white,
                        thickness: 0.2,
                      ),
                      const Padding(
                        padding: EdgeInsets.only(
                            top: 15, bottom: 10, left: 10, right: 10),
                        child: TextField(
                          style: TextStyle(
                              fontWeight: FontWeight.w300, fontSize: 14),
                          decoration: InputDecoration(
                            hintText: 'Click to add description',
                            border: InputBorder.none,
                          ),
                          cursorColor: Colors.white,
                          maxLines: 14,
                          maxLength: 800,
                        ),
                      ),
                      const Divider(
                        indent: 5,
                        endIndent: 5,
                        color: Colors.white,
                        thickness: 0.2,
                      ),
                      const SizedBox(height: 30),
                      GestureDetector(
                        onTap: () {
                          print("Upload script button pressed.");
                        },
                        child: Container(
                            padding: EdgeInsets.only(left: 10),
                            margin: EdgeInsets.only(right: 115),
                            height: 50,
                            child: Card(
                              elevation: 5,
                              child: Row(
                                children: const [
                                  Icon(
                                    Icons.file_present_rounded,
                                    size: 30,
                                  ),
                                  SizedBox(width: 5),
                                  Text("Click to upload your script")
                                ],
                              ),
                            )),
                      ),
                      const SizedBox(height: 30),
                      GestureDetector(
                        onTap: () async {
                          print("object");
                        },
                        child: Container(
                          alignment: Alignment.center,
                          width: MediaQuery.of(context).size.width * 0.9,
                          height: 50,
                          color: Colors.grey[800],
                          child: Text(
                            'Submit.',
                            style: TextStyle(
                                fontSize: 28, fontWeight: FontWeight.bold),
                            textAlign: TextAlign.center,
                          )
                              .animate(
                            onPlay: (controller) =>
                                controller.repeat(reverse: true),
                          )
                              .shimmer(
                              color: Colors.blue,
                              duration: Duration(seconds: 3)),
                        ),
                      )
                    ],
                  )),
            ),
          ),
        ),
      ),
    );
  }
}

CodePudding user response:

Use only titleController which is TextEdiiting controller and titleController.text is string of that textfield which use this controller to control textfiled.

  • Related