仅在我第一次运行它时出错"在空时调用了getter'length'。接收器:空'



所以我尝试构建Sliverlist在第一次运行时,我将儿童计数硬编码为20(例如(,该应用程序运行良好但是,如果我在第一次运行时尝试将childCount设置为FireStore集合的长度,我会得到错误

======== Exception caught by widgets library =======================================================
The following NoSuchMethodError was thrown building:
The getter 'length' was called on null.
Receiver: null
Tried calling: length

但是!如果我首先手动将childCount设置为20,然后对应用程序进行热加载,并将childCount设为handlyCalls.length,它会正常工作,有什么建议吗?

class HandlyCallsList extends StatefulWidget {
@override
_HandlyCallsListState createState() => _HandlyCallsListState();
}
class _HandlyCallsListState extends State<HandlyCallsList> {
@override
Widget build(BuildContext context) {
final handlyCalls = Provider.of<List<HandlyCall>>(context);
int count = 20;
return SliverList(
delegate: SliverChildBuilderDelegate(
(BuildContext context, index) {
print('!!!!!!!!!!!! 11111111111111111111 ${handlyCalls.length}');
return HandlyCallTile(handlyCall: handlyCalls[index]);
},
childCount: handlyCalls.length,
),
);

更新

这是handyCall-to-list功能

List<HandlyCall> _handlyCallListFromSnapshot(QuerySnapshot snapshot) {
return snapshot.docs.map((document) {
return HandlyCall(
title: document.data()['title'] ?? '',
type: document.data()['type'] ?? '',
reward: document.data()['reward'] ?? null,
money: document.data()['money'] ?? 0,
name: document.data()['name'] ?? '',
rating: document.data()['rating'] ?? 25,
user: document.data()['user'] ?? null,
);
}).toList() ;
}
//get userProfile stream
Stream<List<HandlyCall>> get handlyCalls {
return handlyCallsCollection.snapshots().
map(_handlyCallListFromSnapshot);
}

我不确定handlyCalls是什么样的,但我认为对你来说最好的方法是在一个类中创建一个列表,这个类将在提供程序中处理,并包含一个handlyCall列表,而不给提供程序一个handleCalls列表。

所以会是这样的:

class handlyCallsList extends ChangeNotifier {

List<handlyCalls> _list;

List<handlyCalls> get list => _list;

void changeList(List<handlyCalls> list){
this._list = list;
notifyListeners(); // Important to notify all the listeners and make the listeners refresh the view
}
}

现在,您应该使用默认值初始化您的提供程序,这样您就可以始终拥有一个列表,这样它在第一次运行时就不会为null。

ChangeNotifierProvider<handlyCallsList>(
create: (_) => handlyCallsList()..changeList([]), // you can add the default list here for when it loads the first time so you will not find it null
);

您也可以检查该值,看看它是否为空——返回的列表,或者使用handlyCalls?进行检查?。可能的空值的长度。

好吧,经过长时间的尝试和错误,我尝试了一种不同的方法,使用ListView而不是CustomScrollView,它像魔术一样工作,我将chiledCount设置为我的LIST.length,突然之间,在没有错误的情况下,我将代码更改为ListView.Builder,并将Home Scaffold的主体更改为NestedScrollView,并将我的SliverAppBar插入其中,SliverListDelegate 可能有故障

我的新代码:

Scaffold(...
body: NestedScrollView(
floatHeaderSlivers: true,
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
return <Widget>[
SliverAppBar(...),
];
},
body: HandlyCallsList(),
),
),

列表视图生成器

class _HandlyCallsListState extends State<HandlyCallsList> {

@override
Widget build(BuildContext context) {

final handlyCalls = Provider.of<List<HandlyCall>>(context);
return ListView.builder(
itemCount: handlyCalls.length,
itemBuilder: (context, index) {
return HandlyCallTile(handlyCall: handlyCalls[index],);
}
);
}
}

相关内容

最新更新