Created
December 8, 2019 17:49
-
-
Save r3dm1ke/5ba18ae29c1f941e1ae5513772450028 to your computer and use it in GitHub Desktop.
Creating a document in Firestore from Flutter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'package:cloud_firestore/cloud_firestore.dart'; | |
| import 'package:flutter/material.dart'; | |
| class TODOCreate extends StatefulWidget { | |
| @override | |
| State<StatefulWidget> createState() { | |
| return TODOCreateState(); | |
| } | |
| } | |
| class TODOCreateState extends State<TODOCreate> { | |
| final collection = Firestore.instance.collection('tasks'); | |
| final TextEditingController controller = TextEditingController(); | |
| @override | |
| Widget build(BuildContext context) { | |
| return Scaffold( | |
| appBar: AppBar(title: Text('Create a task')), | |
| body: Center( | |
| child: Padding( | |
| padding: EdgeInsets.all(16), | |
| child: TextField( | |
| autofocus: true, | |
| controller: controller, | |
| decoration: InputDecoration( | |
| labelText: 'Enter name for your task' | |
| ) | |
| ) | |
| ) | |
| ), | |
| floatingActionButton: FloatingActionButton( | |
| child: Icon(Icons.done), | |
| onPressed: () async { | |
| // Creating a new document | |
| await collection.add({'name': controller.text, 'completed': false}); | |
| Navigator.pop(context); | |
| }, | |
| ), | |
| ); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'package:firebase_auth/firebase_auth.dart'; | |
| import 'package:flutter/material.dart'; | |
| import 'package:todo_app/auth.dart'; | |
| import 'package:todo_app/login.dart'; | |
| import 'package:todo_app/create.dart'; | |
| import 'package:todo_app/list.dart'; | |
| void main() => runApp(TODOApp()); | |
| class TODOApp extends StatelessWidget { | |
| @override | |
| Widget build(BuildContext context) { | |
| return TODO(); | |
| } | |
| } | |
| class TODO extends StatefulWidget { | |
| @override | |
| State<StatefulWidget> createState() { | |
| return TODOState(); | |
| } | |
| } | |
| class TODOState extends State<TODO> { | |
| final Authentication auth = new Authentication(); | |
| FirebaseUser user; | |
| void onLogin(FirebaseUser user) { | |
| setState(() { | |
| this.user = user; | |
| }); | |
| } | |
| @override | |
| Widget build(BuildContext context) { | |
| return MaterialApp( | |
| title: 'TODO app', | |
| initialRoute: '/', | |
| routes: { | |
| '/': (context) => TODOLogin(onLogin: onLogin), | |
| '/list': (context) => TODOList(), | |
| '/create': (context) => TODOCreate(), | |
| }, | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment