cloud_firestore_all 0.1.1+4 cloud_firestore_all: ^0.1.1+4 copied to clipboard
A Firebase Firestore wrapper for Flutter that wraps cloud_firestore and firebase into a single API for use on both Flutter web and mobile
import 'package:cloud_firestore_all/cloud_firestore_all.dart';
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Firestore Example',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MessageList extends StatelessWidget {
MessageList({this.firestore});
final Firestore firestore;
@override
Widget build(BuildContext context) {
return StreamBuilder<QuerySnapshot>(
stream: firestore.collection('messages').onSnapshot,
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (!snapshot.hasData) return const Text('Loading...');
final int messageCount = snapshot.data.docs.length;
return ListView.builder(
itemCount: messageCount,
itemBuilder: (_, int index) {
final DocumentSnapshot document = snapshot.data.docs[index];
final dynamic message = document['message'];
return ListTile(
title: Text(
message != null ? message.toString() : '<No message retrieved>',
),
subtitle: Text('Message ${index + 1} of $messageCount'),
trailing: Text(
DateTime.fromMillisecondsSinceEpoch(
document['created_at']?.millisecondsSinceEpoch ?? 0,
isUtc: true,
).toString(),
),
);
},
);
},
);
}
}
class MyHomePage extends StatelessWidget {
final Firestore firestore = firestoreInstance;
CollectionReference get messages => firestore.collection('messages');
Future<void> _addMessage() async {
await messages.add(<String, dynamic>{
'message': 'Hello World from Cloud_Firestore_All!',
'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),
),
);
}
}