firestore_all 0.2.0
firestore_all: ^0.2.0 copied to clipboard
Plugin that wraps Firestore from `firebase` and `cloud_firestore` packages and expose them as a single API.
import 'package:firestore_all/firestore_all.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
Firestore firestore;
void main() {
WidgetsFlutterBinding.ensureInitialized();
firestore = setupFirestore(
webApiKey: 'AIzaSyA_oLeZpKm9gX_MszdSGdgIP9PokdqQJw4',
webAuthDomain: 'flutterblocwithprovider.firebaseapp.com',
webDatabaseUrl: 'https://flutterblocwithprovider.firebaseio.com',
webProjectId: 'flutterblocwithprovider',
webStorageBucket: 'flutterblocwithprovider.appspot.com',
);
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: ArticlesList(),
);
}
}
class ArticlesList extends StatelessWidget {
const ArticlesList({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Firestore All Example')),
body: StreamBuilder<List<DocumentSnapshot>>(
stream: firestore
.collection('articles')
.snapshots()
.map((snapshot) => snapshot.documents),
builder: (context, snapshot) {
if (snapshot.hasData) {
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (context, index) {
final data = snapshot.data[index];
return ListTile(
title: Text(data['title']),
subtitle: Text(data['content']),
);
},
);
} else if (snapshot.hasError) {
return Center(child: Text('Error :('));
} else {
return Center(child: CircularProgressIndicator());
}
},
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: () => Navigator.of(context)
.push(MaterialPageRoute(builder: (_) => AddArticlePage())),
),
);
}
}
class AddArticlePage extends HookWidget {
@override
Widget build(BuildContext context) {
final formKey = useMemoized(() => GlobalKey<FormState>());
final scaffoldKey = useMemoized(() => GlobalKey<ScaffoldState>());
final title = useState<String>();
final content = useState<String>();
final onAddClick = () {
formKey.currentState.save();
scaffoldKey.currentState.showSnackBar(
SnackBar(content: Text('Adding...')),
);
firestore.collection('articles').add({
'title': title.value,
'content': content.value,
}).then((_) {
Navigator.of(context).pop();
});
};
return Scaffold(
key: scaffoldKey,
appBar: AppBar(title: Text('Add article')),
body: Form(
key: formKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextFormField(
decoration: InputDecoration(hintText: 'Title'),
onSaved: (value) => title.value = value,
),
TextFormField(
decoration: InputDecoration(hintText: 'Content'),
onSaved: (value) => content.value = value,
)
],
),
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: onAddClick,
),
);
}
}