blaugold / iabtcf_consent_info

Flutter plugin for reading IAB TCF v2.0 user consent information, such as made available through CMP SDKs, like Funding Choices's User Messaging Platform (UMP).
https://pub.dev/packages/iabtcf_consent_info
10 stars 8 forks source link

Error: The getter 'publisherConsents' isn't defined for the class 'BasicConsentInfo' #10

Open chrissy-dev opened 1 year ago

chrissy-dev commented 1 year ago

I can't seem to access publisherConsents in the return. I can print it but I can't access it? Any ideas? I'm using the demo code.

I'd like to create a wrapper that takes a widget, checks for consent and decides whether the it can load.

import 'package:flutter/material.dart';
import 'package:iabtcf_consent_info/iabtcf_consent_info.dart';

class ConsentGate extends StatefulWidget {
  const ConsentGate({Key? key, required this.child}) : super(key: key);

  final Widget child;

  @override
  State<ConsentGate> createState() => _ConsentGateState();
}

class _ConsentGateState extends State<ConsentGate> {
  BasicConsentInfo? consentInfo;

  @override
  void initState() {
    WidgetsBinding.instance.addPostFrameCallback((_) async {
      consentInfo = await _getConsentInfo();
    });
    super.initState();
  }

  Future<BasicConsentInfo?> _getConsentInfo() async {
    BasicConsentInfo? consent =
        await IabtcfConsentInfo.instance.currentConsentInfo();

    setState(() {
      consentInfo = consent;
    });

    return consent;
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Text(consentInfo.toString());
  }
}

I'd like to have a conditional check in the build but I can't access the data. I can see the data being printed but I'm unsure how to access it.

Screenshot 2023-03-20 at 13 27 17

jice-lavocat commented 1 year ago

Try to set the type as ConsentInfo instead of BasicConsentInfo to get access to the relevant variables.

Example:

final info = await IabtcfConsentInfo.instance.currentConsentInfo() as ConsentInfo;
bool hasAdsGranted = info.purposeConsents.contains(DataUsagePurpose.selectBasicAds) ?? false;
print(hasAdsGranted); // should display true or false

It got me some time to figure out, so I posted it for other people who might land here too.