【发布时间】:2021-06-25 21:42:19
【问题描述】:
我收到 NoSuchMethodError: The gettter 'length' was called on null 所以我想知道如何解决这个问题。
当我尝试获取收藏值的长度时发生了问题。
最喜欢的视图模型
class FavoriteViewModel extends BaseViewModel {
List<FavoriteModel> favorites = [];
void initialize(FavoriteService favProvider) {
favorites = favProvider.getFavorites();
}
}
重新排序屏幕
class _ReorderPageState extends State<ReorderPage> {
@override
Widget build(BuildContext context) {
var favProvider = Provider.of<FavoriteService>(context, listen: true);
return BaseView<FavoriteViewModel>(onModelReady: (model) {
model.initialize(favProvider);
}, builder: (context, model, child) {
return model.state == ViewState.Busy
......
Widget reorderWidget(FavoriteViewModel model, BuildContext bcontext) {
return Theme(
data: ThemeData(primaryColor: Colors.transparent),
child: ReorderableListView(
onReorder: (int oldIndex, int newIndex) {
_onParentReorder(oldIndex, newIndex, model);
},
scrollDirection: Axis.vertical,
children: List.generate(
model.favorites.length, // I think the issue is in this line
(index) {
FavoriteModel favorite = model.favorites[index]; // I think the issue is in this line
【问题讨论】:
-
这几乎总是表明未正确管理 Future。这件事的许多答案已经在这里得到了回答。请搜索“flutter call on null”。
标签: flutter dart flutter-layout flutter-dependencies flutter-web