首页 > 解决方案 > 在构建期间调用 setState() 或 markNeedsBuild()。调用原生广告时

问题描述

import 'package:facebook_audience_network/ad/ad_native.dart';
import 'package:facebook_audience_network/facebook_audience_network.dart';
import 'package:flutter/material.dart';
import 'package:share/share.dart';
import 'package:social_share/social_share.dart';
import 'package:clipboard_manager/clipboard_manager.dart';

// ignore: camel_case_types
class listview extends StatefulWidget {
  const listview({
    Key key,
    @required this.records,
  }) : super(key: key);

  final List records;

  @override
  _listviewState createState() => _listviewState();
}

// ignore: camel_case_types
class _listviewState extends State<listview> {
  @override
  void initState() {
    super.initState();

    FacebookAudienceNetwork.init(
      testingId: "35e92a63-8102-46a4-b0f5-4fd269e6a13c",
    );

    // _loadInterstitialAd();
    // _loadRewardedVideoAd();
  }

  // ignore: unused_field
  Widget _currentAd = SizedBox(
    width: 0.0,
    height: 0.0,
  );

  Widget createNativeAd() {
    _showNativeAd() {
      _currentAd = FacebookNativeAd(
        adType: NativeAdType.NATIVE_AD,
        backgroundColor: Colors.blue,
        buttonBorderColor: Colors.white,
        buttonColor: Colors.deepPurple,
        buttonTitleColor: Colors.white,
        descriptionColor: Colors.white,
        width: double.infinity,
        height: 300,
        titleColor: Colors.white,
        listener: (result, value) {
          print("Native Ad: $result --> $value");
        },
      );
    }

    return Container(
      width: double.infinity,
      height: 300,
      child: _showNativeAd(),
    );
  }

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemCount: this.widget.records.length,
      itemBuilder: (BuildContext context, int index) {
        var card = Container(
          child: Card(
            margin: EdgeInsets.only(bottom: 10),
            elevation: 10,
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(1.0),
            ),
            child: Column(
              children: <Widget>[
                Row(
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: <Widget>[
                    Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: GestureDetector(
                        onTap: () {
                          print(this.widget.records);
                        },
                        child: Image.asset(
                          "assets/icons/avatar.png",
                          height: 45,
                        ),
                      ),
                    ),
                    Padding(
                      padding: const EdgeInsets.only(left: 5),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          Text(
                            (this
                                .widget
                                .records[index]['fields']['Publisher Name']
                                .toString()),
                            style: TextStyle(fontWeight: FontWeight.bold),
                          ),
                          Text(
                            (this
                                .widget
                                .records[index]['fields']['Date']
                                .toString()),
                            style: TextStyle(
                              fontSize: 12,
                            ),
                          ),
                        ],
                      ),
                    )
                  ],
                ),
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Container(
                    width: MediaQuery.of(context).size.width * 0.90,
                    // height: MediaQuery.of(context).size.height * 0.20,
                    decoration: BoxDecoration(
                      border: Border.all(
                        color: Colors.purple,
                        width: 3.0,
                      ),
                    ),
                    child: Center(
                      child: Padding(
                        padding: const EdgeInsets.all(18.0),
                        child: Text(
                          (this
                              .widget
                              .records[index]['fields']['Shayari']
                              .toString()),
                          style: TextStyle(
                            color: Colors.green,
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                      ),
                    ),
                  ),
                ),
                SizedBox(
                  height: 5,
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: <Widget>[
                    GestureDetector(
                      onTap: () {
                        ClipboardManager.copyToClipBoard(this
                                .widget
                                .records[index]['fields']['Shayari']
                                .toString())
                            .then((result) {
                          final snackBar = SnackBar(
                            content: Text('Copied to Clipboard'),
                          );
                          Scaffold.of(context).showSnackBar(snackBar);
                        });
                        print(this
                            .widget
                            .records[index]['fields']['Shayari']
                            .toString());
                      },
                      child: Image.asset(
                        "assets/icons/copy.png",
                        height: 25,
                      ),
                    ),
                    GestureDetector(
                      onTap: () async {
                        SocialShare.checkInstalledAppsForShare().then(
                          (data) {
                            print(data.toString());
                          },
                        );
                      },
                      child: Image.asset(
                        "assets/icons/whatsapp.png",
                        height: 35,
                      ),
                    ),
                    GestureDetector(
                      onTap: () async {
                        Share.share("Please Share https://google.com");
                      },
                      child: Image.asset(
                        "assets/icons/share.png",
                        height: 25,
                      ),
                    ),
                  ],
                ),
                SizedBox(
                  height: 5,
                ),
              ],
            ),
          ),
        );
        return Column(
          children: [
            card,
            index != 0 && index % 5 == 0
                ? /* Its Show When Value True*/ createNativeAd()
                : /* Its Show When Value false*/ Container()
          ],
        );
      },
    );
  }
}

我在构建期间调用了此错误 setState() 或 markNeedsBuild()。 在此处输入图像描述

我尝试在我的动态列表视图之间调用 Facebook 原生广告,但是当我调用原生广告createNativeAd时显示此错误这是我设置原生广告的小部件这是我调用广告的代码 ** return Column( children: [ card, index ! = 0 && index % 5 == 0 ? /* 当值为真时显示*/ createNativeAd() : /* 当值为假时显示*/ Container() ], ); }, );**

标签: flutterlistviewnative

解决方案


推荐阅读