Flutter报错:参数类型‘String’无法赋值给‘TextEditingController?’求解决
Flutter错误修复:The argument type 'String' can't be assigned to the parameter type 'TextEditingController?'
问题根源
- 参数类型不匹配:
TextField的controller参数要求传入TextEditingController实例,你错误地传入了titleController.text(字符串类型)。 - const组件引用非const变量:你给目标
TextField添加了const修饰符,但titleController是State中初始化的非const变量,const组件无法访问非const成员,这也是titleController.text提示“getter未定义”的直接原因。
修复步骤
- 移除
TextField的const关键字; - 将
controller参数的值从titleController.text改为titleController(直接传入控制器实例)。
完整修正代码
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: const 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: const Column( children: [ SizedBox(height: 50), Icon( Icons.video_file_rounded, size: 56, ), SizedBox(height: 5), Text( "Upload Video", style: TextStyle(fontWeight: FontWeight.bold), ) ], ), ), )), Padding( padding: const EdgeInsets.only( top: 15, bottom: 10, left: 10, right: 10), child: TextField( controller: titleController, style: const TextStyle( fontWeight: FontWeight.bold, fontSize: 22), decoration: const 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: const EdgeInsets.only(left: 10), margin: const EdgeInsets.only(right: 115), height: 50, child: Card( elevation: 5, child: const Row( children: [ 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: const TextStyle( fontSize: 28, fontWeight: FontWeight.bold), textAlign: TextAlign.center, ) .animate( onPlay: (controller) => controller.repeat(reverse: true), ) .shimmer( color: Colors.blue, duration: const Duration(seconds: 3)), ), ) ], )), ), ), ), ), ); } }
内容的提问来源于stack exchange,提问作者Muhammed Shoaib
相关产品推荐
相关产品推荐

