text stringlengths 1 474 |
|---|
<code_start>WidgetsFlutterBinding.ensureInitialized(); |
await Firebase.initializeApp( |
options: DefaultFirebaseOptions.currentPlatform, |
);<code_end> |
This ensures that Firebase is initialized on game startup.Add the Firestore instance to the app. |
That way, any widget can access this instance. |
Widgets can also react to the instance missing, if needed.To do this with the card template, you can use |
the provider package |
(which is already installed as a dependency).Replace the boilerplate runApp(MyApp()) with the following: |
<code_start>runApp( |
Provider.value( |
value: FirebaseFirestore.instance, |
child: MyApp(), |
), |
);<code_end> |
Put the provider above MyApp, not inside it. |
This enables you to test the app without Firebase.info Note |
In case you are not working with the card template, |
you must either install the provider package |
or use your own method of accessing the FirebaseFirestore |
instance from various parts of your codebase.<topic_end> |
<topic_start> |
4. Create a Firestore controller class |
Though you can talk to Firestore directly, |
you should write a dedicated controller class |
to make the code more readable and maintainable.How you implement the controller depends on your game |
and on the exact design of your multiplayer experience. |
For the case of the card template, |
you could synchronize the contents of the two circular playing areas. |
It’s not enough for a full multiplayer experience, |
but it’s a good start.To create a controller, copy, |
then paste the following code into a new file called |
lib/multiplayer/firestore_controller.dart. |
<code_start>import 'dart:async'; |
import 'package:cloud_firestore/cloud_firestore.dart'; |
import 'package:flutter/foundation.dart'; |
import 'package:logging/logging.dart'; |
import '../game_internals/board_state.dart'; |
import '../game_internals/playing_area.dart'; |
import '../game_internals/playing_card.dart'; |
class FirestoreController { |
static final _log = Logger('FirestoreController'); |
final FirebaseFirestore instance; |
final BoardState boardState; |
/// For now, there is only one match. But in order to be ready |
/// for match-making, put it in a Firestore collection called matches. |
late final _matchRef = instance.collection('matches').doc('match_1'); |
late final _areaOneRef = _matchRef |
.collection('areas') |
.doc('area_one') |
.withConverter<List<PlayingCard>>( |
fromFirestore: _cardsFromFirestore, toFirestore: _cardsToFirestore); |
late final _areaTwoRef = _matchRef |
.collection('areas') |
.doc('area_two') |
.withConverter<List<PlayingCard>>( |
fromFirestore: _cardsFromFirestore, toFirestore: _cardsToFirestore); |
StreamSubscription? _areaOneFirestoreSubscription; |
StreamSubscription? _areaTwoFirestoreSubscription; |
StreamSubscription? _areaOneLocalSubscription; |
StreamSubscription? _areaTwoLocalSubscription; |
FirestoreController({required this.instance, required this.boardState}) { |
// Subscribe to the remote changes (from Firestore). |
_areaOneFirestoreSubscription = _areaOneRef.snapshots().listen((snapshot) { |
_updateLocalFromFirestore(boardState.areaOne, snapshot); |
}); |
_areaTwoFirestoreSubscription = _areaTwoRef.snapshots().listen((snapshot) { |
_updateLocalFromFirestore(boardState.areaTwo, snapshot); |
}); |
// Subscribe to the local changes in game state. |
_areaOneLocalSubscription = boardState.areaOne.playerChanges.listen((_) { |
_updateFirestoreFromLocalAreaOne(); |
}); |
_areaTwoLocalSubscription = boardState.areaTwo.playerChanges.listen((_) { |
_updateFirestoreFromLocalAreaTwo(); |
}); |
_log.fine('Initialized'); |
} |
void dispose() { |
_areaOneFirestoreSubscription?.cancel(); |
_areaTwoFirestoreSubscription?.cancel(); |
_areaOneLocalSubscription?.cancel(); |
_areaTwoLocalSubscription?.cancel(); |
_log.fine('Disposed'); |
} |
/// Takes the raw JSON snapshot coming from Firestore and attempts to |
/// convert it into a list of [PlayingCard]s. |
List<PlayingCard> _cardsFromFirestore( |
DocumentSnapshot<Map<String, dynamic>> snapshot, |
SnapshotOptions? options, |
) { |
final data = snapshot.data()?['cards'] as List?; |
if (data == null) { |
_log.info('No data found on Firestore, returning empty list'); |
return []; |
} |
final list = List.castFrom<Object?, Map<String, Object?>>(data); |
try { |
return list.map((raw) => PlayingCard.fromJson(raw)).toList(); |
} catch (e) { |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.