代码:
void _ads() async {
var interAd = InterstitialAd(
adUnitId: "use_some_genuine_ad_id_not_test_one",
);
await interAd.load();
await interAd.show();
}
Run Code Online (Sandbox Code Playgroud)
问题:
有时Admob会加载带有视频的插页式广告,并且该广告的默认音量设置为最大,有什么办法可以将其静音吗?
对于奖励视频广告,我找不到任何静音广告的选项,有人可以帮忙吗?
我正在使用http库来下载图像。
final client = http.Client();
final _response = await client.send(http.Request('GET', Uri.parse("my_url")));
Run Code Online (Sandbox Code Playgroud)
听音方式一:(听音法)
int downloaded = 0;
_response.stream.listen((value) {
// this gets called 82 times and I receive actual image size
downloaded += value.length;
});
Run Code Online (Sandbox Code Playgroud)
监听方式2:(StreamBuilder widget)
int downloaded = 0;
StreamBuilder<List<int>>(
stream: _response.stream,
builder: (_, snapshot) {
// this gets called 11 times and I receive around 1/10 actual image size
if (snapshot.hasData) downloaded += snapshot.data.length;
return Container();
},
);
Run Code Online (Sandbox Code Playgroud)
问题是为什么当新数据到达时StreamBuilder'sbuild()方法没有被频繁调用,它根本就违背了用作小部件的目的。
我有:
class _PageState extends State<Page> with WidgetsBindingObserver {
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state);
print('state = $state');
}
@override
Widget build(BuildContext context) => Scaffold();
}
Run Code Online (Sandbox Code Playgroud)
AppLifeCycleState类有 4 个回调,其中 3 个
- active
- paused
- resumed
Run Code Online (Sandbox Code Playgroud)
似乎有效,但detached无论如何都没有起作用。
我阅读了文档,但在实际场景中无法理解它,任何人都可以分享相关代码,何时何地调用它?
我可以忽略这样的单个变量的规则:
class Class {
// ignore: prefer_final_fields
var _count = 0;
// ignore: prefer_final_fields
var _count2 = 0;
}
Run Code Online (Sandbox Code Playgroud)
我想使用这样的东西,但它不起作用。如何忽略整个班级的 lint?
// ignore: prefer_final_fields (DOESN'T WORK)
class Class {
var _count = 0;
var _count2 = 0;
}
Run Code Online (Sandbox Code Playgroud) 我是从 Web 背景来到 Flutter 的,在那里我习惯于根据屏幕的高度和宽度的百分比或包含它们的元素来定义屏幕元素。
我刚刚完成了一个课程。
现在我很热情并想开始构建一个应用程序,我有点困惑,因为课程只谈到了绝对像素值的高度和宽度。我可以看到这对于不同的纵横比是有问题的,尤其是不同的方向。
对此有规范的方法吗?官方文档似乎也使用绝对像素值,所以也许我错过了一个基本点。
搜索表明我可能会使用MediaQuery,然后根据它缩放所有内容。但是,我没有看到在代码示例中广泛使用它。
是否有无意见的标准方法?
这就是我要的。
int _foo = 42;
int get foo => _foo;
Run Code Online (Sandbox Code Playgroud)
但是当我使用 dartfmt 格式化代码时,它会变成
int _foo = 42;
int get foo => _foo;
Run Code Online (Sandbox Code Playgroud)
我怎样才能防止这种行为?
为了提供自定义动画,我可以扩展MaterialPageRoute或PageRouteBuilder,它们似乎都做同样的工作。那么,两者有什么区别,我应该使用哪一种呢?
这是代码片段:
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: ElevatedButton(
onPressed: () => Navigator.push(context, Foo/BarRoute(Scaffold(appBar: AppBar()))),
child: Text('Navigate'),
),
);
}
}
class FooRoute<T> extends MaterialPageRoute<T> {
final Widget page;
FooRoute(this.page) : super(builder: (_) => page);
@override
Widget buildTransitions(_, animation, __, ___) {
return FadeTransition(opacity: animation, child: page);
}
}
class BarRoute<T> extends PageRouteBuilder<T> {
final Widget page;
BarRoute(this.page) : super(pageBuilder: (_, __, ___) => page);
@override
Widget buildTransitions(_, animation, …Run Code Online (Sandbox Code Playgroud) 我能够像文档中提到的那样创建一个notification_keyfrom 。notification_key_name
但是,当我notification_key从notification_key_name使用中检索
我收到错误:
您的客户发出了格式错误或非法的请求。
我究竟做错了什么?
我有:
Future<bool> foo() async => true;
Run Code Online (Sandbox Code Playgroud)
这是允许的:
Future<void> bar() {
return foo(); // Works
}
Run Code Online (Sandbox Code Playgroud)
但这不是:
Future<void> baz() async {
return foo(); // Error
}
Run Code Online (Sandbox Code Playgroud)
在两者中bar,baz我都返回 a Future<bool>,但为什么第一个有效但第二个失败?
注意:这个问题不是关于如何让它工作,而是关于为什么一个有效而另一个无效。
我用于firebase_auth电话号码验证。这是最少的代码。
Future<void> foo() async {
await FirebaseAuth.instance.verifyPhoneNumber(
verificationFailed: (_) {
throw Exception('Foo'); // It throws an exception.
},
phoneNumber: '+1650',
verificationCompleted: (_) {},
codeSent: (_, __) {},
codeAutoRetrievalTimeout: (_) {},
);
}
Run Code Online (Sandbox Code Playgroud)
我像这样调用上面的函数
void main() {
foo().catchError(print); // But this code fails to catch it.
}
Run Code Online (Sandbox Code Playgroud)
但catchError似乎没有发现错误,我遇到了:
[VERBOSE-2:shell.cc(93)] Dart 未处理的异常:异常:foo
那么,我如何正确捕获该异常呢?