使用DropDownTextField组件出现LateInitializationError问题的解决方法
解决DropDownTextField的LateInitializationError问题
问题描述
使用DropDownTextField下拉文本字段组件时触发LateInitializationError,错误提示:
LateInitializationError: Field '_listTileTextStyle@76295511' has not been initialized.
报错栈信息:
LateInitializationError: Field '_listTileTextStyle@76295511' has not been initialized. When the exception was thrown, this was the stack: #0 _DropDownTextFieldState._listTileTextStyle (package:dropdown_textfield/dropdown_textfield.dart) #1 _DropDownTextFieldState.updateFunction.<anonymous closure> (package:dropdown_textfield/dropdown_textfield.dart:455:41) dropdown_textfield.dart:455 #2 SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1392:15) binding.dart:1392 #3 SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1326:11) binding.dart:1326 #4 SchedulerBinding.scheduleWarmUpFrame.<anonymous closure> (package:flutter/src/scheduler/binding.dart:1035:9) binding.dart:1035 #5 PlatformDispatcher.scheduleWarmUpFrame.<anonymous closure> (dart:ui/platform_dispatcher.dart:837:16) platform_dispatcher.dart:837 #9 _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12) isolate_patch.dart:184 (elided 3 frames from class _Timer and dart:async-patch)
使用的组件代码:
DropDownTextField( textStyle: TextStyle( color: Colors.white, ), listTextStyle: TextStyle( color: Colors.grey, ), controller: gendercontroller, clearOption: true, dropdownColor: Colors.black, textFieldDecoration: InputDecoration( hintText: gender, border: InputBorder.none, fillColor: Colors.blue, ), validator: (value) { if (value == null) { return "Required field"; } else { print(gendercontroller .dropDownValue!.name); return null; } }, dropDownItemCount: 2, dropDownList: const [ DropDownValueModel( name: 'Male', value: "Male"), DropDownValueModel( name: 'Female', value: "Female", ), ], onChanged: (val) { setState(() { print(val); }); }, )
解决方案
1. 升级组件包到最新版本
该错误属于dropdown_textfield包内部的变量初始化bug,新版本已修复。执行以下命令升级:
flutter pub upgrade dropdown_textfield
2. 临时修复(若无法升级)
- 给
listTextStyle补充完整样式属性,避免默认值处理异常:listTextStyle: TextStyle( color: Colors.grey, fontSize: 14, // 添加该属性 ), - 确认
gendercontroller已正确初始化:final gendercontroller = DropDownController();
内容的提问来源于stack exchange,提问作者Muhammedali M A
相关产品推荐
相关产品推荐

