参数类型 'dynamic Function(AppLovinAdListener)' 无法分配给参数类型 'dynamic Function(AppLovinAdListener?)'
The argument type 'dynamic Function(AppLovinAdListener)' can't be assigned to the parameter type 'dynamic Function(AppLovinAdListener?)'
我正在尝试将 Applovin Interstitial 广告添加到 flutter 应用程序。但它一直显示错误。我的 sdk 是 sdk:'>=2.12.0 <3.0.0'.
这是显示的错误
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?)'.
这是示例代码:
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,
),
),
],
),
),
),
),
下面是代码错误的图片
bool 是一个严格的布尔值 - 它可以是 true 或 false,没有别的。
布尔?是一个松散的布尔值或也称为可空 - 它可以是真、假或空。
函数也一样。
插件'AppLovin'我假设你正在使用或者你声明的包,或者你声明的函数,不支持null-safety,这意味着它的功能 can return null.
为了解决这个问题,您需要通过以下方式使类型 nullable在类型声明后添加一个问号(即 bool a;
将变为 - bool? a;
并确保你 不要在 null 上调用方法。因为你的变量和方法 can return null,编辑器会事先通知你这个(变量)可能是 null 并调用一个方法它将抛出 MethodNotFoundException
。因此,您应该添加自己的逻辑以确保不会发生这种情况,并通过 添加 null-checks(感叹号)来满足编译器的要求标记 - '!') 到方法调用,它作为一种形式向编译器断言此值 不为 null,尽管它是 nullable。 如果碰巧您在 运行 处通过空检查对值调用了空时间(在您按下 运行 并加载您的页面后)您将得到以下异常 - Null-check used on a null value.
我正在尝试将 Applovin Interstitial 广告添加到 flutter 应用程序。但它一直显示错误。我的 sdk 是 sdk:'>=2.12.0 <3.0.0'.
这是显示的错误
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?)'.
这是示例代码:
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,
),
),
],
),
),
),
),
下面是代码错误的图片
bool 是一个严格的布尔值 - 它可以是 true 或 false,没有别的。
布尔?是一个松散的布尔值或也称为可空 - 它可以是真、假或空。
函数也一样。
插件'AppLovin'我假设你正在使用或者你声明的包,或者你声明的函数,不支持null-safety,这意味着它的功能 can return null.
为了解决这个问题,您需要通过以下方式使类型 nullable在类型声明后添加一个问号(即 bool a;
将变为 - bool? a;
并确保你 不要在 null 上调用方法。因为你的变量和方法 can return null,编辑器会事先通知你这个(变量)可能是 null 并调用一个方法它将抛出 MethodNotFoundException
。因此,您应该添加自己的逻辑以确保不会发生这种情况,并通过 添加 null-checks(感叹号)来满足编译器的要求标记 - '!') 到方法调用,它作为一种形式向编译器断言此值 不为 null,尽管它是 nullable。 如果碰巧您在 运行 处通过空检查对值调用了空时间(在您按下 运行 并加载您的页面后)您将得到以下异常 - Null-check used on a null value.