I'm trying to add Applovin Interstitial ads to flutter app. But it keep showing errors. my sdk is sdk:'>=2.12.0 <3.0.0'.
This are the errors displayed
A value of type 'bool?' can't be assigned to a variable of type 'bool'. Try changing the type of the variable, or casting the right-hand type to 'bool'.
The argument type 'dynamic Function(AppLovinAdListener)' can't be assigned to the parameter type 'dynamic Function(AppLovinAdListener?)'.
This is the sample code:
const CollectionCard();
@override
State<CollectionCard> createState() => _CollectionCardState();
}
class _CollectionCardState extends State<CollectionCard> {
AppLovinListener? get listener => null;
void initState() {
FlutterApplovinMax.initInterstitialAd('91b26a7777e1b455');
super.initState();
}
bool isInterstitialVideoAvailable = false;
@override
Widget build(BuildContext context) {
var size = MediaQuery.of(context).size;
/*24 is for notifications bar on Android */
final double itemHeight = (size.height - kToolbarHeight - 28) / 2;
final double itemWidth = size.width / 4;
return Container(
child: Padding(
padding: const EdgeInsets.fromLTRB(10.0, 0.0, 10.0, 10.0),
child: Column(
children: <Widget>[
GridView.count(
primary: true,
padding: const EdgeInsets.fromLTRB(20, 0, 20, 20),
crossAxisSpacing: 10, //Reduce Horizontal Spacing
mainAxisSpacing: 10, //Reduce Vertical Spacing
crossAxisCount: 3,
physics: ScrollPhysics(),
childAspectRatio: (6 / 8),
// (itemWidth / itemHeight),
shrinkWrap: true,
children: <Widget>[
Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15.0),
),
elevation: 2,
color: Theme.of(context).scaffoldBackgroundColor,
child: InkWell(
onTap: () async {
isInterstitialVideoAvailable =
await FlutterApplovinMax.isInterstitialLoaded (listener);
if (isInterstitialVideoAvailable) {
FlutterApplovinMax.showInterstitialVideo(
(AppLovinAdListener event) => listener (event));
}
Navigator.push(
context,
MaterialPageRoute(
builder: (ctx) => LearnPage(),
),
);
},
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
ImageIcon(
AssetImage('assets/icons/learn.png'),
color: kLightPrimary,
size: 60,
), // Icon(
// layout.icon,
// size: 40,
// ),
Text(
'Learn',
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.bold,
),
),
],
),
),
),
),
Below is an image of the code error
bool is a strict boolean - it can be true or false, nothing else.
bool? is a loose boolean or also called nullable - it can be true, false or null.
Same goes for the function.
The Plugin 'AppLovin' I assume you are using or the package, or the function you declared, doesn't support null-safety, which means that it's functions can return null.
In order to solve the issue, you need to make the types nullable by adding a question mark after the type declaration (i.e. bool a;
will become - bool? a;
and make sure you don't call a method on a null. Since your variables and methods can return null, the editor will inform you before hand that this (the variable) might be null and calling a method on it will throw a MethodNotFoundException
. Therefore, you should add your own logic to assure that this doesn't happen and satisfy the compiler by adding null-checks (exclamation marks - '!') to method calls which works as a form of asserting the compiler that this value isn't null, despite it being nullable. If it so happens that you do call a null on a value with a null check, at run time (after you press the run and load your page) you will get the following exception - Null-check used on a null value.