text
stringlengths 1
474
|
|---|
@override
|
void dispose() {
|
_bannerAd?.dispose();
|
super.dispose();
|
}
|
/// Loads a banner ad.
|
void _loadAd() {
|
final bannerAd = BannerAd(
|
size: widget.adSize,
|
adUnitId: widget.adUnitId,
|
request: const AdRequest(),
|
listener: BannerAdListener(
|
// Called when an ad is successfully received.
|
onAdLoaded: (ad) {
|
if (!mounted) {
|
ad.dispose();
|
return;
|
}
|
setState(() {
|
_bannerAd = ad as BannerAd;
|
});
|
},
|
// Called when an ad request failed.
|
onAdFailedToLoad: (ad, error) {
|
debugPrint('BannerAd failed to load: $error');
|
ad.dispose();
|
},
|
),
|
);
|
// Start loading.
|
bannerAd.load();
|
}
|
}<code_end>
|
lightbulb Tip
|
In many cases, you will want to load the ad outside a widget.For example, you can load it in a ChangeNotifier, a BLoC, a controller,
|
or whatever else you are using for app-level state. This way, you can
|
preload a banner ad in advance, and have it ready to show for when the
|
user navigates to a new screen.Verify that you have loaded the BannerAd instance before showing it with
|
an AdWidget, and that you dispose of the instance when it is no longer
|
needed.
|
<topic_end>
|
<topic_start>Add multiplayer support using Firestore
|
Multiplayer games need a way to synchronize game states between players.
|
Broadly speaking, two types of multiplayer games exist:High tick rate.
|
These games need to synchronize game states many times per second
|
with low latency.
|
These would include action games, sports games, fighting games.Low tick rate.
|
These games only need to synchronize game states occasionally
|
with latency having less impact.
|
These would include card games, strategy games, puzzle games.This resembles the differentiation between real-time versus turn-based
|
games, though the analogy falls short.
|
For example, real-time strategy games run—as the name suggests—in
|
real-time, but that doesn’t correlate to a high tick rate.
|
These games can simulate much of what happens
|
in between player interactions on local machines.
|
Therefore, they don’t need to synchronize game states that often.If you can choose low tick rates as a developer, you should.
|
Low tick lowers latency requirements and server costs.
|
Sometimes, a game requires high tick rates of synchronization.
|
For those cases, solutions such as Firestore don’t make a good fit.
|
Pick a dedicated multiplayer server solution such as Nakama.
|
Nakama has a Dart package.If you expect that your game requires a low tick rate of synchronization,
|
continue reading.This recipe demonstrates how to use the
|
cloud_firestore package
|
to implement multiplayer capabilities in your game.
|
This recipe doesn’t require a server.
|
It uses two or more clients sharing game state using Cloud Firestore.<topic_end>
|
<topic_start>
|
1. Prepare your game for multiplayer
|
Write your game code to allow changing the game state
|
in response to both local events and remote events.
|
A local event could be a player action or some game logic.
|
A remote event could be a world update coming from the server.To simplify this cookbook recipe, start with
|
the card template that you’ll find
|
in the flutter/games repository.
|
Run the following command to clone that repository:Open the project in templates/card.info Note
|
You can ignore this step and follow the recipe with your own game
|
project. Adapt the code at appropriate places.<topic_end>
|
<topic_start>
|
2. Install Firestore
|
Cloud Firestore is a horizontally scaling,
|
NoSQL document database in the cloud.
|
It includes built-in live synchronization.
|
This is perfect for our needs.
|
It keeps the game state updated in the cloud database,
|
so every player sees the same state.If you want a quick, 15-minute primer on Cloud Firestore,
|
check out the following video:To add Firestore to your Flutter project,
|
follow the first two steps of the
|
Get started with Cloud Firestore guide:The desired outcomes include:You don’t need to write any Dart code in this step.
|
As soon as you understand the step of writing
|
Dart code in that guide, return to this recipe.<topic_end>
|
<topic_start>
|
3. Initialize Firestore
|
Open lib/main.dart and import the plugins,
|
as well as the firebase_options.dart file
|
that was generated by flutterfire configure in the previous step.
|
<code_start>import 'package:cloud_firestore/cloud_firestore.dart';
|
import 'package:firebase_core/firebase_core.dart';
|
import 'firebase_options.dart';<code_end>
|
Add the following code just above the call to runApp()
|
in lib/main.dart:
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.