如何在 flutter 中使用分页显示 listview.builder 中的原生广告?

2024-02-18

我使用下面的代码来检索颤振应用程序中的列表,使用分页和 firestore 作为数据库,工作正常。我指的是flutter_native_admob原生广告的依赖性。但我不知道如何在listview.builder同时我需要实现分页。 就像在 Instagram 中一样,在发布一定数量的帖子后,原生广告就会出现,我需要以类似的方式显示添加内容。这怎么可能?

  _getProducts() async {
    query = Firestore.instance.collection("users").document(uid).collection('my_favorites').orderBy("timestamp", descending: true).limit(5);
    setState(() {

      _loadingnotifications = true ;
    });

    QuerySnapshot _querySnapshot = await query.getDocuments();


    _lastDocument = _querySnapshot.documents[_querySnapshot.documents.length - 1];

    _notifications = _querySnapshot.documents;
    setState(() {
      _loadingnotifications = false;
    });

  }

  _getMoreNotifications() async {
    print("new docs loaded");
    if(_moreProductsAvailable == false ){
      return;
    }

    if(_loadingMore == true ){

      return;
    }

    _loadingMore = true;
  
    query = Firestore.instance.collection("users").document(uid).collection('my_favorites').orderBy("timestamp", descending: true).startAfter([_lastDocument.data['timestamp']]).limit(5);
    QuerySnapshot _querySnapshot = await query.getDocuments();

    if (_querySnapshot.documents.length <5){
      _moreProductsAvailable = false;
    }
    _lastDocument = _querySnapshot.documents[_querySnapshot.documents.length - 1];

    _notifications.addAll(_querySnapshot.documents);
  
    setState(() {
          _loadingnotifications = false;
    });

    _loadingMore = false;


  }

列表视图生成器

Flexible(
                          child:_loadingnotifications == true ? Container(child: Center(child: Text("Loading..."),),) :Container(child:  _notifications.length == 0? Center(
                          child: Text("No Marked Posts Yet!"),) :
             new ListView.builder(
              controller: _scrollController,
              itemCount: _notifications.length,
              itemBuilder: (BuildContext ctx, int index){


                String itemTitle = _notifications[index].data['itemTitle'];
               

                              return ItemCard(itemTitle: itemTitle,                         );

              }),)
                        ),

我知道如何创建 admob 帐户并初始化flutter_native_admob在应用程序中,但不知道如何将其放入列表中,就像 Instagram 和分页一样。


您可以在循环中添加一个计数器变量,在最终列表中添加小部件之前,您可以检查每当计数器变量是 5 的倍数时,添加一个广告小部件,否则添加一个普通小部件,这将在每 5 个之后在您的列表中添加广告普通小部件。

您可以检查一下以供参考:https://medium.com/@vitopiegari/embedding-ads-into-flutters-widget-tree-with-admob-flutter-ae59c3a66492 https://medium.com/@vitopiegari/embedding-ads-into-flutters-widget-tree-with-admob-flutter-ae59c3a66492

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 flutter 中使用分页显示 listview.builder 中的原生广告? 的相关文章

随机推荐