0

I am trying to display a bannerAd between every 3 posts. But ads do not show. I presume, that the list could be empty, as this list is a list build from the posts user likes and saves.

@override
Widget build(BuildContext context) {
return Expanded(
    child: ListView(
  padding: EdgeInsets.symmetric(horizontal: 10.0),
  children: [
    StreamBuilderWrapper(
    shrinkWrap: true,
    stream: postRef
        .where("bookmarks",
        arrayContains: FirebaseAuth.instance.currentUser.uid)
        .orderBy('timestamp', descending: true)
        .snapshots(),
    physics: NeverScrollableScrollPhysics(),
    itemBuilder: (_, DocumentSnapshot snapshot) {
      internetChecker(context);
      Review posts = Review.fromJson(snapshot.data());
      return Padding(
        padding: const EdgeInsets.only(bottom: 10.0),
        child: Posts(post: posts),
          );
    },
 ),
    ListView.builder(
        scrollDirection: Axis.vertical,
        shrinkWrap: true,
        itemBuilder: (context, index) {
          return Column(
            children: [
              if (index % 3 == 0 && index != 0)
                AdWidget(ad: ad)
            ],);})
  ]));
}

This is my first time working with flutter, and i am new to coding. I am trying to display the bannerAds, however, they do not show and no log is displayed. This is probably due to the render error I have:

The following _CastError was thrown during paint():
Null check operator used on a null value

The relevant error-causing widget was: 
 ListView 
When the exception was thrown, this was the stack: 
#0      RenderViewportBase._paintContents 
(package:flutter/src/rendering/viewport.dart:653:25)
#1      RenderViewportBase.paint 
(package:flutter/src/rendering/viewport.dart:645:7)
#2      RenderObject._paintWithContext 
(package:flutter/src/rendering/object.dart:2317:7)
#3      PaintingContext._repaintCompositedChild 
(package:flutter/src/rendering/object.dart:139:11)
#4      PaintingContext.repaintCompositedChild 
(package:flutter/src/rendering/object.dart:100:5)
...
The following RenderObject was being processed when the exception was 
fired: 
RenderViewport#ae00e
...  needs compositing
...  parentData: <none> (can use size)
...  constraints: BoxConstraints(w=360.0, h=507.0)
 ...  layer: OffsetLayer#0dfc7 DETACHED
...    engine layer: Null#007db
...    offset: Offset(0.0, 0.0)
...  size: Size(360.0, 507.0)
...  axisDirection: down
...  crossAxisDirection: right
...  offset: ScrollPositionWithSingleContext#1c5d2(offset: 0.0, range: 
 null..null, viewport: 507.0, ScrollableState, 
AlwaysScrollableScrollPhysics 
-> ClampingScrollPhysics -> RangeMaintainingScrollPhysics, 
IdleScrollActivity#40f4c, ScrollDirection.idle)

I am confused with the widgets and views. Help me, please! Thank you!

han da
  • 95
  • 9

1 Answers1

0

i didn't understand clearly what you re doing, i assume you re doing this wrong way,I edited the whole code this may help .

@override
Widget build(BuildContext context) {
return Expanded(
  child: StreamBuilder(
      stream: postRef
          .where("bookmarks",
          arrayContains: FirebaseAuth.instance.currentUser!.uid)
          .orderBy('timestamp', descending: true)
          .snapshots(),


      builder: (_, DocumentSnapshot snapshot) {
if (snapshot.hasData) {


return ListView.separated(itemCount: snapshot.data.docs.length,
itemBuilder: (
context,
index,
) {

internetChecker(context);
Review posts = Review.fromJson(snapshot.data());
return Padding(
padding: const EdgeInsets.only(bottom: 10.0),
child: Posts(post: posts),
);
}, separatorBuilder: (context, index) {
if (index % 3 == 0 && index != 0)
return

AdWidget(ad: ad);
}, );

  }else if (snapshot.hasError) {
  return Center(
  child:  Text("error"),


  );
 } else {
 return  Center(
   child:  Text("loading"),


 );
 }

  },
    ),);
Kafil khan
  • 81
  • 2
  • 5
  • I see where I missed the logic! However, now am getting the renderbox not laid out issue. will figure that out! Thanks again! :) – han da Jul 27 '21 at 07:00