cloud_firestore_mocks 0.9.0+1 cloud_firestore_mocks: ^0.9.0+1 copied to clipboard
Fake implementation of Cloud Firestore. Use this package to write unit tests involving Cloud Firestore.
// Copyright 2017, the Chromium project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:async';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:flutter_driver/driver_extension.dart';
Future<void> main() async {
enableFlutterDriverExtension();
WidgetsFlutterBinding.ensureInitialized();
final app = await Firebase.initializeApp(
name: 'test',
options: const FirebaseOptions(
appId: '1:79601577497:ios:5f2bcc6ba8cecddd',
messagingSenderId: '79601577497',
apiKey: 'AIzaSyArgmRGfB5kiQT6CunAOmKRVKEsxKmy6YI-G72PVU',
projectId: 'flutter-firestore',
),
);
final firestore = FirebaseFirestore.instanceFor(app: app);
runApp(MaterialApp(
title: 'Firestore Example', home: MyHomePage(firestore: firestore)));
}
class MessageList extends StatelessWidget {
MessageList({required this.firestore});
final FirebaseFirestore firestore;
@override
Widget build(BuildContext context) {
return StreamBuilder<QuerySnapshot>(
stream: firestore.collection('messages').snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
final data = snapshot.data;
if (!snapshot.hasData || data == null) return const Text('Loading...');
final messageCount = data.docs.length;
return ListView.builder(
itemCount: messageCount,
itemBuilder: (_, int index) {
final document = data.docs[index];
final dynamic message = document.get('message');
return ListTile(
title: Text(
message != null ? message.toString() : '<No message retrieved>',
),
subtitle: Text('Message ${index + 1} of $messageCount'),
);
},
);
},
);
}
}
class MyHomePage extends StatelessWidget {
MyHomePage({required this.firestore});
final FirebaseFirestore firestore;
CollectionReference get messages => firestore.collection('messages');
Future<void> _addMessage() async {
await messages.add(<String, dynamic>{
'message': 'Hello world!',
'created_at': FieldValue.serverTimestamp(),
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Firestore Example'),
),
body: MessageList(firestore: firestore),
floatingActionButton: FloatingActionButton(
onPressed: _addMessage,
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
}