Posts

Flutter Firestore Run Transaction to make your UpVoting counter with FieldValue.arrayUnion

 Code: import 'package:flutter/material.dart' ; import 'package:firebase_auth/firebase_auth.dart' ; import 'package:cloud_firestore/cloud_firestore.dart' ; var myDocId, numberOfUpVoters; void addUpVotingField() async { List<String> emptyList = new List (); await FirebaseFirestore. instance .collection( 'YourCollection' ).add({ "OtherField" : "OtherValue" , //... more fields "upVoters" : FieldValue. arrayUnion (emptyList), //to store uid of users who up-voted }).then((DocumentReference documentReference) { myDocId = documentReference. id ; }); } void firestoreTransaction() { User user = FirebaseAuth. instance . currentUser ; DocumentReference documentReference = FirebaseFirestore. instance .collection( 'YourCollection' ).doc(myDocId); FirebaseFirestore. instance .runTransaction((transaction) async { DocumentSnapshot documentSnapshot = await transaction.get(...

Flutter: call a method from another class / setState a page from outside that class with StreamController

Image
This Flutter demo shows how to call a method from another class or how to setState a page from outside that class, using StreamController. main.dart import 'package:flutter/material.dart' ; import 'dart:async' ; StreamController<int> streamController = StreamController <int>(); void main() { runApp( MyApp ()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp ( title: 'Flutter Demo' , theme: ThemeData ( primarySwatch: Colors. blue , ), home: MyHomePage ( 'Flutter Demo Home Page' , streamController. stream ), ); } } class MyHomePage extends StatefulWidget { MyHomePage( this . title , this . stream ); final String title ; final Stream<int> stream ; @override _MyHomePageState createState() => _MyHomePageState (); } class _MyHomePageState extends State<MyHomePage> { String menuName = 'A' ; @override void i...

Flutter Firebase Auth Email and Google Signin With Lastest Oct 2020 Update

Image
Update: Nov 11, 2020 Added phone_login.dart for phone number authentication.     Update: Nov 2, 2020 1. Added "forgot password" to email_login.dart 2. Added forgot_password.dart   Update: Nov 1, 2020 1. Added "await" in email_singup.dart 2. Added "isLoading = false;" in email_login.dart   As of Oct 7 2020, Firebase Auth for Flutter has the new version 0.18.1+2. This Flutter Auth demo with Email and Google Sign-in is just a new update to show you how to work with the newest Firebase Auth version. Most of the codes here are obtained from Peter Haddad 's article How To Use Firebase Authentication In Flutter. Many thanks to Peter Haddad .   1. Create a new Flutter project.  Open Android Studio. And create a new Flutter project named "fire_auth_demo". Then go to android/app/build.gradle. Look for "applicationId". That's your Android package name. My package name is "com.haoc.fire_auth_demo". Yours might be different. We nee...