Skip to content

Instantly share code, notes, and snippets.

@creativecreatorormaybenot
Last active November 12, 2021 10:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save creativecreatorormaybenot/8f90899dbe5841a1780e1dce27ca6a23 to your computer and use it in GitHub Desktop.
Save creativecreatorormaybenot/8f90899dbe5841a1780e1dce27ca6a23 to your computer and use it in GitHub Desktop.
Firestore in Dartpad
// https://twitter.com/creativemaybeno/status/1458523044557770755?s=20
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
Future<void> main() async {
await Firebase.initializeApp(
options: const FirebaseOptions(
apiKey: 'AIzaSyCEfiLLknrsFhk6VCUdKKvOjrWEaJp61uQ',
authDomain: 'dart-pad-counter.firebaseapp.com',
projectId: 'dart-pad-counter',
storageBucket: 'dart-pad-counter.appspot.com',
messagingSenderId: '391018863530',
appId: '1:391018863530:web:416db91d2502a0179ee9d3',
),
);
runApp(const CounterApp());
}
class CounterApp extends StatelessWidget {
const CounterApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Firestore Counter demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const Counter(),
);
}
}
class Counter extends StatelessWidget {
const Counter({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text(
'The world has pressed the button this many times:',
),
StreamBuilder<DocumentSnapshot>(
stream: FirebaseFirestore.instance
.doc('collection/doc')
.snapshots(),
builder: (context, snapshot) {
return Text(
snapshot.data?.get('number')?.toString() ?? '?',
style: Theme.of(context).textTheme.headline4,
);
}),
Image.network('https://i.imgur.com/AoXaWBY.png'),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
FirebaseFirestore.instance
.doc('collection/doc')
.update({'number': FieldValue.increment(1)});
},
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment