#flutter #dart
#трепетать #дротик
Вопрос:
Вот itemCount: querySnapshot.size,
здесь querySnapshot.docs[index].data()['image'],
и здесь onTap: () =gt; gotoPage(querySnapshot.docs[index].data()['title'])),
.size
, .docs
и .docs
есть эта ошибка:
К свойству «документы» нельзя получить безусловный доступ, потому что получатель может быть «нулевым». Попробуйте сделать доступ условным (используя»?.») или добавить нулевую проверку к цели («!»).
Когда я положу ! или ? слова, которые говорит ошибка:
«Размер» геттера не определен для типа «Объект». Попробуйте импортировать библиотеку, которая определяет «размер», исправив имя на имя существующего получателя или определив получатель или поле с именем «размер».
Вот полный код:
@override Widget build(BuildContext context) { return Scaffold( drawer: NavDrawer(), body: SafeArea( child: Container( child: StreamBuilder( stream: query.snapshots(), builder: (context, snapshot) { if (snapshot.connectionState == ConnectionState.waiting) { return Center( child: CircularProgressIndicator(), ); } else if (snapshot.hasError) { return Center( child: Icon(Icons.error), ); } final Object? querySnapshot = snapshot.data; return ListView( physics: BouncingScrollPhysics(), children: lt;Widgetgt;[ Container( margin: EdgeInsets.only(top: 28.8, bottom: 16.8), height: 724.8, child: ListView.builder( itemCount: querySnapshot.size, padding: EdgeInsets.only(left: 28.8, right: 12), scrollDirection: Axis.vertical, physics: BouncingScrollPhysics(), itemBuilder: (context, index) { return Container( height: 214.8, width: 188.4, margin: EdgeInsets.only(right: 16.8, bottom: 50), decoration: BoxDecoration( borderRadius: BorderRadius.circular(9.6), image: DecorationImage( fit: BoxFit.cover, image: CachedNetworkImageProvider( querySnapshot.docs[index].data()['image'], maxHeight: 200, maxWidth: 200), ), ), child: Stack( children: lt;Widgetgt;[ GestureDetector( onTap: () =gt; gotoPage(querySnapshot .docs[index] .data()['title'])),
Ответ №1:
вам нужно обнулить проверки, например, использовать ?.
или !
@override Widget build(BuildContext context) { return Scaffold( drawer: NavDrawer(), body: SafeArea( child: Container( child: StreamBuilder( stream: query.snapshots(), builder: (context, snapshot) { if (snapshot.connectionState == ConnectionState.waiting) { return Center( child: CircularProgressIndicator(), ); } else if (snapshot.hasError) { return Center( child: Icon(Icons.error), ); } final Object? querySnapshot = snapshot.data; return ListView( physics: BouncingScrollPhysics(), children: lt;Widgetgt;[ Container( margin: EdgeInsets.only(top: 28.8, bottom: 16.8), height: 724.8, child: ListView.builder( itemCount: querySnapshot?.length, // here change needed padding: EdgeInsets.only(left: 28.8, right: 12), scrollDirection: Axis.vertical, physics: BouncingScrollPhysics(), itemBuilder: (context, index) { return Container( height: 214.8, width: 188.4, margin: EdgeInsets.only(right: 16.8, bottom: 50), decoration: BoxDecoration( borderRadius: BorderRadius.circular(9.6), image: DecorationImage( fit: BoxFit.cover, image: CachedNetworkImageProvider( querySnapshot?.docs[index]?.data() ['image'], // here change needed maxHeight: 200, maxWidth: 200), ), ), child: Stack( children: lt;Widgetgt;[ GestureDetector( onTap: () =gt; gotoPage(querySnapshot ?.docs[index] ?.data()['title'])),//// here change needed
Комментарии:
1. Когда я это делаю, я увидел эту ошибку:
The getter 'size' isn't defined for the type 'Object'. Try importing the library that defines 'size', correcting the name to the name of an existing getter, or defining a getter or field named 'size'.
2. вместо размера вы должны использовать длину
3. Та же ошибка:
The getter 'length' isn't defined for the type 'Object'. Try importing the library that defines 'length', correcting the name to the name of an existing getter, or defining a getter or field named 'length'.
4. измените тип объекта или замените на
snapshot.data.length
в количестве элементов5. Я заменил это
snapshot.data.length
наquerySnapshot?.size
. Но я вижу ту же ошибку