【发布时间】:2019-12-11 03:46:47
【问题描述】:
我有以下代码。
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'My App',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: SimpleScreen());
}
}
class SimpleScreen extends StatefulWidget {
@override
_SimpleScreenState createState() => _SimpleScreenState();
}
class _SimpleScreenState extends State<SimpleScreen> {
ItemCls currentValue = ItemCls(name: 'one', price: 1);
List<DropdownMenuItem> _menuItems = <DropdownMenuItem>[
DropdownMenuItem(
child: new Container(
child: new Text("Item#1"),
width: 200.0,
),
value: ItemCls(name: 'one', price: 1)),
DropdownMenuItem(
child: new Container(
child: new Text("Item#2"),
width: 200.0,
),
value: ItemCls(name: 'two', price: 2))
];
@override
Widget build(BuildContext context) {
return new Scaffold(
body: Center(
child: DropdownButton(
value: currentValue,
items: _menuItems,
onChanged: onChanged,
style: Theme.of(context).textTheme.title,
),
));
}
void onChanged(value) {
setState(() {
currentValue = value;
});
// print(value);
}
}
class ItemCls {
final String name;
final double price;
const ItemCls({
@required this.name,
@required this.price,
}) : assert(name != null),
assert(price != null);
}
失败了
在构建 SimpleScreen(dirty, 依赖项:[_LocalizationsScope-[GlobalKey#b1ff3], _InheritedTheme],状态:_SimpleScreenState#d473f):'package:flutter/src/material/dropdown.dart':断言失败:行 620 位置 15: '项目 == 空 ||项目.isEmpty ||值 == 空 || items.where((DropdownMenuItem item) => item.value == value).length == 1':不正确。
【问题讨论】: