{"id":1789,"date":"2024-03-04T10:13:57","date_gmt":"2024-03-04T10:13:57","guid":{"rendered":"https:\/\/www.devopssupport.in\/blog\/?p=1789"},"modified":"2024-03-04T10:13:59","modified_gmt":"2024-03-04T10:13:59","slug":"error-in-flutter-method-countbuilder-was-called-on-nullerror-in-flutter","status":"publish","type":"post","link":"https:\/\/www.devopssupport.in\/blog\/error-in-flutter-method-countbuilder-was-called-on-nullerror-in-flutter\/","title":{"rendered":"Error in Flutter: &#8220;Method &#8216;countBuilder&#8217; was called on null&#8221;Error in Flutter:"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"409\" height=\"477\" src=\"https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2024\/03\/image-30.png\" alt=\"\" class=\"wp-image-1790\" srcset=\"https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2024\/03\/image-30.png 409w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2024\/03\/image-30-257x300.png 257w\" sizes=\"auto, (max-width: 409px) 100vw, 409px\" \/><\/figure>\n\n\n\n<p>Error:<\/p>\n\n\n\n<p>\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550 Exception caught by widgets library \u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550 The method &#8216;countBuilder&#8217; was called on null. Receiver: null Tried calling: countBuilder(crossAxisCount: 4, crossAxisSpacing: 4, itemBuilder: Closure: (BuildContext, int) =&gt; DoctorBox, itemCount: 246, mainAxisSpacing: 4, physics: Instance of &#8216;NeverScrollableScrollPhysics&#8217;, shrinkWrap: true, staggeredTileBuilder: Closure: (int) =&gt; dynamic)<\/p>\n\n\n\n<p>The relevant error-causing widget was:<\/p>\n\n\n\n<p>\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550<\/p>\n\n\n\n<p>In my case:<\/p>\n\n\n\n<p>The error message &#8220;Method &#8216;countBuilder&#8217; was called on null&#8221; typically occurs when a method or property is being accessed on a null object. In Flutter, this often happens when trying to access properties or methods of widgets that haven&#8217;t been properly initialized or when attempting to use widgets that are null.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (filteredDoctors.isNotEmpty)\n\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 getDoctorList()\n\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 else if (searchController.text.isNotEmpty)\n\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 const Center(\n\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 child: Text('No matching doctors found'),\n\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 ),<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>getDoctorList() {\r\n    final filteredDoctors = searchController.text.isNotEmpty\r\n        ? searchDoctors(searchController.text)\r\n        : doctors;\r\n\r\n    return StaggeredGridView.countBuilder(\r\n      shrinkWrap: true,\r\n      physics: const NeverScrollableScrollPhysics(),\r\n      crossAxisCount: 4,\r\n      itemCount: filteredDoctors.length,\r\n      itemBuilder: (BuildContext context, int index) => DoctorBox(\r\n        onTap: () {\r\n          Navigator.push(\r\n            context,\r\n            MaterialPageRoute(\r\n              builder: (BuildContext context) =>\r\n                  DoctorProfilePage(doctor: filteredDoctors&#91;index]),\r\n            ),\r\n          );\r\n        },\r\n        index: index,\r\n        doctor: filteredDoctors&#91;index],\r\n      ),\r\n      staggeredTileBuilder: (int index) =>\r\n          StaggeredTile.count(2, index.isEven ? 2 : 3),\r\n      mainAxisSpacing: 4,\r\n      crossAxisSpacing: 4,\r\n    );\r\n  }<\/code><\/pre>\n\n\n\n<p>After changing<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>      \/\/ Display either the StaggeredGridView or a \"No data available\" message\r\n            if (filteredDoctors.isNotEmpty)\r\n              getDoctorList(filteredDoctors)\r\n            else if (searchController.text.isNotEmpty)\r\n              const Center(\r\n                child: Text('No matching doctors found'),\r\n              ),\r\n          ],\r\n        ),\r\n      ),\r\n    );\r\n  }\r\n\r\n  Widget getDoctorList(List&lt;dynamic> filteredDoctors) {\r\n    final filteredDoctors = searchController.text.isNotEmpty\r\n        ? searchDoctors(searchController.text)\r\n        : doctors;\r\n\r\n    return StaggeredGridView.countBuilder(\r\n      shrinkWrap: true,\r\n      physics: const NeverScrollableScrollPhysics(),\r\n      crossAxisCount: 4,\r\n      itemCount: filteredDoctors.length,\r\n      itemBuilder: (BuildContext context, int index) => DoctorBox(\r\n        onTap: () {\r\n          Navigator.push(\r\n            context,\r\n            MaterialPageRoute(\r\n              builder: (BuildContext context) =>\r\n                  DoctorProfilePage(doctor: filteredDoctors&#91;index]),\r\n            ),\r\n          );\r\n        },\r\n        index: index,\r\n        doctor: filteredDoctors&#91;index],\r\n      ),\r\n      staggeredTileBuilder: (int index) =>\r\n          StaggeredTile.count(2, index.isEven ? 2 : 3),\r\n      mainAxisSpacing: 4,\r\n      crossAxisSpacing: 4,\r\n    );\r\n  }<\/code><\/pre>\n\n\n\n<p>hope<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Error: \u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550 Exception caught by widgets library \u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550 The method &#8216;countBuilder&#8217; was called on null. Receiver: null Tried calling: countBuilder(crossAxisCount: 4, crossAxisSpacing: 4, itemBuilder: Closure: (BuildContext, int)&#8230; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1789","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/1789","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/comments?post=1789"}],"version-history":[{"count":1,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/1789\/revisions"}],"predecessor-version":[{"id":1791,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/1789\/revisions\/1791"}],"wp:attachment":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/media?parent=1789"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/categories?post=1789"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/tags?post=1789"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}