我在颤振中正确运行 multi_select_flutter 小部件时遇到问题
Posted
技术标签:
【中文标题】我在颤振中正确运行 multi_select_flutter 小部件时遇到问题【英文标题】:I am having problem running multi_select_flutter widget properly in flutter 【发布时间】:2022-01-04 07:50:20 【问题描述】:您好,我正在尝试multi_select_flutter
,但无法解决此错误
“List
这是我正在使用的multi_select_flutter
小部件的代码
MultiSelectBottomSheetField(
initialChildSize: 0.4,
listType: MultiSelectListType.CHIP,
searchable: true,
buttonText: Text("Favorite Animals"),
title: Text("Animals"),
items: _items,
onConfirm: (values)
// _selectedAnimals2 = values;
,
chipDisplay: MultiSelectChipDisplay(
onTap: (value)
setState(()
_selectedAnimals2.remove(value);
);
,
),
),
我尝试将 _selectedAnimals2 更改为类型转换对象,然后此错误消失但我无法进一步使用它
我尝试在 onConfirm 中打印值,但它只显示 Animal 的实例,但我无法访问与动物类相关的任何字段
这里是完整代码
class Animal
final int? id;
final String? name;
Animal(
this.id,
this.name,
);
我在这里声明列表
static final List<Animal> _animals = [
Animal(id: 1, name: "Lion"),
Animal(id: 2, name: "Flamingo"),
Animal(id: 3, name: "Hippo"),
Animal(id: 4, name: "Horse"),
Animal(id: 5, name: "Tiger"),
Animal(id: 6, name: "Penguin"),
Animal(id: 7, name: "Spider"),
Animal(id: 8, name: "Snake"),
Animal(id: 9, name: "Bear"),
Animal(id: 10, name: "Beaver"),
Animal(id: 11, name: "Cat"),
Animal(id: 12, name: "Fish"),
Animal(id: 13, name: "Rabbit"),
Animal(id: 14, name: "Mouse"),
Animal(id: 15, name: "Dog"),
Animal(id: 16, name: "Zebra"),
Animal(id: 17, name: "Cow"),
Animal(id: 18, name: "Frog"),
Animal(id: 19, name: "Blue Jay"),
Animal(id: 20, name: "Moose"),
Animal(id: 21, name: "Gecko"),
Animal(id: 22, name: "Kangaroo"),
Animal(id: 23, name: "Shark"),
Animal(id: 24, name: "Crocodile"),
Animal(id: 25, name: "Owl"),
Animal(id: 26, name: "Dragonfly"),
Animal(id: 27, name: "Dolphin"),
];
final _items = _animals
.map((animal) => MultiSelectItem<Animal>(animal, animal.name!))
.toList();
List<Animal?> _selectedAnimals2 = [];
final _multiSelectKey = GlobalKey<FormFieldState>();
这里是代码
Container(
width: 0.9.sw,
height: 150.0.h,
decoration: const BoxDecoration(
color: Colors.white24,
borderRadius: BorderRadius.all(
Radius.circular(10),
),
),
child: MultiSelectBottomSheetField(
initialChildSize: 0.4,
listType: MultiSelectListType.CHIP,
searchable: true,
buttonText: const Text("Favorite Animals"),
title: const Text("Animals"),
items: _items,
onConfirm: (values)
_selectedAnimals2 = values;
,
chipDisplay: MultiSelectChipDisplay(
onTap: (value)
setState(()
_selectedAnimals2.remove(value);
);
,
),
),
),
// _selectedAnimals2 == null || _selectedAnimals2.isEmpty
// ? Container(
// padding: EdgeInsets.all(10),
// alignment: Alignment.centerLeft,
// child: Text(
// "None selected",
// style: TextStyle(color: Colors.black54),
// ))
// : Container(),
【问题讨论】:
【参考方案1】:_items 的元素应该是不可为空的,因为项目声明如下:
类型不匹配是您遇到错误的原因。
你错过了类型参数,检查上面的截图。
【讨论】:
“List 我试过这种方法,但没有奏效 在这里粘贴你的完整代码,也许我可以帮忙:) 我已经更新了问题请告诉我哪里错了 你错过了类型参数,检查我编辑的答案。在MultiSelectBottomSheetField上添加类型,就可以了。以上是关于我在颤振中正确运行 multi_select_flutter 小部件时遇到问题的主要内容,如果未能解决你的问题,请参考以下文章
我在 android studio 中运行我的颤振项目并尝试使用 iPhone 12 模拟器,但 Xcode 构建每次都失败