text
stringlengths 1
372
|
|---|
future<void> _updateFirestoreFromLocal(
|
PlayingArea area, DocumentReference<List<PlayingCard>> ref) async {
|
try {
|
_log.fine('Updating firestore with local data (${area.cards}) ...');
|
await ref.set(area.cards);
|
_log.fine('... done updating.');
|
} catch (e) {
|
throw FirebaseControllerException(
|
'failed to update firestore with local data (${area.cards}): $e');
|
}
|
}
|
/// sends the local state of `boardstate.areaone` to firestore.
|
void _updateFirestoreFromLocalAreaOne() {
|
_updateFirestoreFromLocal(boardState.areaOne, _areaOneRef);
|
}
|
/// sends the local state of `boardstate.areatwo` to firestore.
|
void _updateFirestoreFromLocalAreaTwo() {
|
_updateFirestoreFromLocal(boardState.areaTwo, _areaTwoRef);
|
}
|
/// updates the local state of [area] with the data from firestore.
|
void _updateLocalFromFirestore(
|
PlayingArea area, DocumentSnapshot<List<PlayingCard>> snapshot) {
|
_log.fine('Received new data from firestore (${snapshot.data()})');
|
final cards = snapshot.data() ?? [];
|
if (listequals(cards, area.cards)) {
|
_log.fine('No change');
|
} else {
|
_log.fine('Updating local data with firestore data ($cards)');
|
area.replaceWith(cards);
|
}
|
}
|
}
|
class FirebaseControllerException implements exception {
|
final string message;
|
FirebaseControllerException(this.message);
|
@override
|
string toString() => 'firebasecontrollerexception: $message';
|
}
|
<code_end>
|
notice the following features of this code:
|
the controller’s constructor takes a BoardState.
|
this enables the controller to manipulate the local state of the game.
|
the controller subscribes to both local changes to update firestore
|
and to remote changes to update the local state and UI.
|
the fields _areaOneRef and _areaTwoRef are
|
firebase document references.
|
they describe where the data for each area resides,
|
and how to convert between the local dart objects (list<playingcard>)
|
and remote JSON objects (map<string, dynamic>).
|
the firestore API lets us subscribe to these references
|
with .snapshots(), and write to them with .set().
|
<topic_end>
|
<topic_start>
|
5. use the firestore controller
|
open the file responsible for starting the play session:
|
lib/play_session/play_session_screen.dart in the case of the
|
card template.
|
you instantiate the firestore controller from this file.
|
import firebase and the controller:
|
<code_start>
|
import 'package:cloud_firestore/cloud_firestore.dart';
|
import '../multiplayer/firestore_controller.dart';
|
<code_end>
|
add a nullable field to the _PlaySessionScreenState class
|
to contain a controller instance:
|
<code_start>
|
FirestoreController? _firestoreController;
|
<code_end>
|
in the initState() method of the same class,
|
add code that tries to read the FirebaseFirestore instance
|
and, if successful, constructs the controller.
|
you added the FirebaseFirestore instance to main.dart
|
in the initialize firestore step.
|
<code_start>
|
final firestore = context.read<FirebaseFirestore?>();
|
if (firestore == null) {
|
_log.warning("Firestore instance wasn't provided. "
|
'running without _firestoreController.');
|
} else {
|
_firestoreController = FirestoreController(
|
instance: firestore,
|
boardState: _boardState,
|
);
|
}
|
<code_end>
|
dispose of the controller using the dispose() method
|
of the same class.
|
<code_start>
|
_firestoreController?.dispose();
|
<code_end>
|
<topic_end>
|
<topic_start>
|
6. test the game
|
run the game on two separate devices
|
or in 2 different windows on the same device.
|
watch how adding a card to an area on one device
|
makes it appear on the other one.
|
open the firebase web console
|
and navigate to your project’s firestore database.
|
watch how it updates the data in real time.
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.