Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

flutter firestore collection snapshots queries tutorial

import 'package:flutter/material.dart';

import 'package:cloud_firestore/cloud_firestore.dart';

void main() => runApp(new MediaQuery(
    data: new MediaQueryData(), child: new MaterialApp(home: new MyApp())));

class MyApp extends StatelessWidget {
  final databaseReference = Firestore.instance;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('FireStore Demo'),
      ),
      body: Center(
          child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: <Widget>[
          RaisedButton(
            child: Text('Create Record'),
            onPressed: () {
              createRecord();
            },
          ),
          RaisedButton(
            child: Text('View Record'),
            onPressed: () {
              getData();
            },
          ),
          RaisedButton(
            child: Text('Update Record'),
            onPressed: () {
              updateData();
            },
          ),
          RaisedButton(
            child: Text('Delete Record'),
            onPressed: () {
              deleteData();
            },
          ),
        ],
      )), //center
    );
  }

  void createRecord() async {
    await databaseReference.collection("books")
        .document("1")
        .setData({
          'title': 'Mastering Flutter',
          'description': 'Programming Guide for Dart'
        });

    DocumentReference ref = await databaseReference.collection("books")
        .add({
          'title': 'Flutter in Action',
          'description': 'Complete Programming Guide to learn Flutter'
        });
    print(ref.documentID);
  }

  void getData() {
    databaseReference
        .collection("books")
        .getDocuments()
        .then((QuerySnapshot snapshot) {
      snapshot.documents.forEach((f) => print('${f.data}}'));
    });
  }

  void updateData() {
    try {
      databaseReference
          .collection('books')
          .document('1')
          .updateData({'description': 'Head First Flutter'});
    } catch (e) {
      print(e.toString());
    }
  }

  void deleteData() {
    try {
      databaseReference
          .collection('books')
          .document('1')
          .delete();
    } catch (e) {
      print(e.toString());
    }
  }
}
Comment

PREVIOUS NEXT
Code Example
Typescript :: marine traffic embeeded map in basic html 
Typescript :: Use AuthGuard with gRPC Metadata 
Typescript :: ngbcollapse error with Reactive Forms 
Typescript :: res.write prints html tags as text in express 
Typescript :: how to send alert notifications to multiple destinations 
Typescript :: whats the next sonic game 
Typescript :: bibtex remove brackets in note field 
Typescript :: python unix get 5 minuts from now 
Typescript :: how to read web page in type script 
Typescript :: Return first k terms from vector 
Typescript :: nullish coalescing angular example 
Typescript :: how to exclude certain proprty from a class typescript 
Typescript :: typescript directory structure 
Typescript :: i like 
Typescript :: routerextensions nativescript 7 import 
Typescript :: ExtractElementByIdFromString(HTMLString, IdString) 
Typescript :: typescript list 
Typescript :: module svg typescript 
Typescript :: how to add type using map in typescript 
Typescript :: spritesheets in pyqt 
Cpp :: hello world c++ 
Cpp :: c++ - include all libraries 
Cpp :: arduino for command 
Cpp :: how to print items in arduino 
Cpp :: on component end overlap c++ 
Cpp :: fatal error: opencv2/opencv.hpp: No such file or directory 
Cpp :: repeat character n times c++ 
Cpp :: cpp executing without console 
Cpp :: C++ std::async wait is taking forever 
Cpp :: did greeks write c++ codes? 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =