"The declaration 'setState' isn't referenced." StatefulWidget 中的 DropdownButton 警告
"The declaration 'setState' isn't referenced." warning for DropdownButton within StatefulWidget
我有一个有状态的小部件,我在其中放置了 DropdownButton。在 DropdownButton 的 onChange 事件中,我有一个 setState 但它一直警告我未引用声明 'setState'。
实际上,无论何时触发 onChange 事件,它都不会被调用。
如有任何建议,我们将不胜感激。
请参阅下面的代码片段以了解更多详细信息。
class TypeSetup extends StatefulWidget {
const TypeSetup({Key? key}) : super(key: key);
@override
_TypeSetupState createState() => _TypeSetupState();
}
class _TypeSetupState extends State<TypeSetup> {
var _selectedType;
List<Type>? _types;
@override
void initState() {
super.initState();
loadTypeJson();
}
Future loadTypeJson() async {
String typeJson = await DefaultAssetBundle.of(context)
.loadString("assets/data/types.json");
Iterable typeIter = json.decode(typeJson)["accommodation-types"];
setState(() {
_types = List<Type>.from(typeIter.map((model) => Type.fromJson(model)));
});
}
@override
Widget build(BuildContext context) {
final typeList = _types?.map((model) {
return new DropdownMenuItem<String>(
value: model.type,`enter code here`
child: new Text(model.name),
);
}).toList();
return Container(
padding: EdgeInsets.all(DefaultPalette.defaultPadding),
decoration: BoxDecoration(
color: DefaultPalette.secondaryColor,
borderRadius: const BorderRadius.all(Radius.circular(10)),
),
child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
Text(
"Set up",
style: Theme.of(context).textTheme.subtitle1,
),
SizedBox(
width: double.infinity,
child: Center(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Container(
padding: EdgeInsets.only(left: 16, right: 16),
decoration: BoxDecoration(
border: Border.all(color: Colors.grey, width: 1),
borderRadius: BorderRadius.circular(13)),
child: DropdownButton(
hint: Text('Select Type'),
dropdownColor: Colors.white,
icon: Icon(Icons.arrow_drop_down),
iconSize: 30,
isExpanded: true,
underline: SizedBox(),
style: TextStyle(color: Colors.black, fontSize: 22),
value: _selectedType,
onChanged: (newValue) {
setState() {
_selectedType = newValue;
print("Selected type changed.");
}
},
items: typeList,
),
)
)
)
)
]),
);
}
}
你需要这样调用setState
setState(() {
_selectedType = newValue;
});
我有一个有状态的小部件,我在其中放置了 DropdownButton。在 DropdownButton 的 onChange 事件中,我有一个 setState 但它一直警告我未引用声明 'setState'。 实际上,无论何时触发 onChange 事件,它都不会被调用。
如有任何建议,我们将不胜感激。
请参阅下面的代码片段以了解更多详细信息。
class TypeSetup extends StatefulWidget {
const TypeSetup({Key? key}) : super(key: key);
@override
_TypeSetupState createState() => _TypeSetupState();
}
class _TypeSetupState extends State<TypeSetup> {
var _selectedType;
List<Type>? _types;
@override
void initState() {
super.initState();
loadTypeJson();
}
Future loadTypeJson() async {
String typeJson = await DefaultAssetBundle.of(context)
.loadString("assets/data/types.json");
Iterable typeIter = json.decode(typeJson)["accommodation-types"];
setState(() {
_types = List<Type>.from(typeIter.map((model) => Type.fromJson(model)));
});
}
@override
Widget build(BuildContext context) {
final typeList = _types?.map((model) {
return new DropdownMenuItem<String>(
value: model.type,`enter code here`
child: new Text(model.name),
);
}).toList();
return Container(
padding: EdgeInsets.all(DefaultPalette.defaultPadding),
decoration: BoxDecoration(
color: DefaultPalette.secondaryColor,
borderRadius: const BorderRadius.all(Radius.circular(10)),
),
child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
Text(
"Set up",
style: Theme.of(context).textTheme.subtitle1,
),
SizedBox(
width: double.infinity,
child: Center(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Container(
padding: EdgeInsets.only(left: 16, right: 16),
decoration: BoxDecoration(
border: Border.all(color: Colors.grey, width: 1),
borderRadius: BorderRadius.circular(13)),
child: DropdownButton(
hint: Text('Select Type'),
dropdownColor: Colors.white,
icon: Icon(Icons.arrow_drop_down),
iconSize: 30,
isExpanded: true,
underline: SizedBox(),
style: TextStyle(color: Colors.black, fontSize: 22),
value: _selectedType,
onChanged: (newValue) {
setState() {
_selectedType = newValue;
print("Selected type changed.");
}
},
items: typeList,
),
)
)
)
)
]),
);
}
}
你需要这样调用setState
setState(() {
_selectedType = newValue;
});