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)),
),
)
],
)),
),
),
),
),
);
}
}
5
Answers
in your TextField you have to just change the controller properties and assign the controller
Here
with
titleController
is an object of classTextController()
andcontroller
parameter requires object ofTextController
class, you can access the data inside usingtitleController.text
, and thus cannot pass it as String in the controller parameterjust set correctly your controller like this
now it will work hopefully. I have removed the const keyword in 66 no line.
Use only titleController which is TextEdiiting controller and titleController.text is string of that textfield which use this controller to control textfiled.