我正在尝试在我的 flutter 应用程序中放置广告,该应用程序显示来自 firebase 的项目流。为此,我使用插入方法,但每次都会收到错误; 错误:参数类型“Text”无法分配给参数类型“DocumentSnapshot”。
这就是我的做法
StreamBuilder(
stream: stream,
//stream: getposts(),
builder: (BuildContext context,
AsyncSnapshot<List<DocumentSnapshot>>snapshots) {
var adcode=Text(" Ad goes here");
snapshots.data.insert(2, adcode);
return Flexible(
child: Container(
child: ListView.builder(
//listview items)
)
有什么想法可以解决上述错误,或者有什么其他方法可以在流构建器的项目之间每隔 N 个项目放置广告吗?我正在使用 Google Admob 软件包来转换广告
最佳答案
实现此目的的一种方法是创建一个新的List
,然后像这样修改其内容。
var adcode = Text(" Ad goes here");
List<dynamic> finalList = snapshots.data;
finalList.insert(2, adcode);
// Use this finalList to build your Widget.
然后,如果您使用 ListView.builder
,每当您到达第 n
索引时,您就可以读取数据并以不同的方式使用它,因为该索引处的数据是将是一个 Text
小部件,而不是您的 DocumentSnapshot
。
关于Flutter如何在streambuilder中每N次添加一个项目到 ListView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67763146/