Flutter & Firebase App Tutorial #7 - Streams

Net Ninja · Beginner ·🛠️ AI Tools & Apps ·6y ago

Key Takeaways

Implements streams in Flutter and Firebase to listen for authentication changes

Full Transcript

alright than my friends so currently we can sign in anonymously inside the signing widget and then we receive the result back which is a user object a custom user object and we can print out the UID of that user now what we really want to be able to do at this point is listen for that authentication change when we get that user and then show the home screen when that happens when a user signs in instead of just printing this to the console so then in the future if they then sign out we can also listen for that authentication change and then we can show them the Sign In screen again so we're protecting our home screen from unauthenticated users now remember inside our wrapper widget right here we can either show the home screen widget or the authenticate one or right here currently showing the authenticate one but we want this to be dynamic so when we're listening to those authentication changes this is going to be the place where we dynamically change what a user sees will it be the authenticate widget or will it be the home widget so this will change dependent on that result so first of all we need to be able to actually listen to those authentic ation changes and to do that we'll be using a stream so then what is a stream a stream is pretty much what it says it is it's a stream so you can think of it as like a real live stream that travels from one place a to another point B and on that stream you could play something at the source which would ultimately then travel to the destination along the stream so imagine that you wanted to say transport 50 rubber ducks from the source to the destination but you also had to pack each duck inside a box first of all so what you could do is you could box up all of the Ducks first and stockpile them and then send them all in one go when they're all boxed up together but that would mean waiting a significant amount of time over here before you get anything at all because you have to wait for them all to be boxed up now the good thing about streams is that we wouldn't have to wait for everything to be boxed up first of all each duck could be sent along the stream as it was boxed up meaning that at the source we'd have a constant income of box docks as they're being made now a similar thing is going to happen if we set up a string between our flutter app and the firebase off service the firebase off service is going to emit something to us every time a user either signs in or signs out and that something could add to be a null value if they signed out or some kind of user object if they signed in so elf flutter up is going to be able to receive those event objects when they happen and determine based on the value inside of them whether they're a user object or null whether a user is logged in on lockdown and at that moment we want to update our UI appropriately now the firebase Earth service has a stream built into it that we can listen to by invoking a function called on earth changed so we're going to take a look at that now and set it up inside our auth service class okay then so we want to set up this stream which is going to detect authentication changes and we want to do that inside our earth service class right here now I'm going to do it above this sign in a non function and I'm going to create a little comment to say what this is and it's just earth change user stream so let's set up this stream first of all it's a type stream and the data that we're going to be getting back from this stream is going to be a firebase user that's what it's a stream of okay because when a user signs in they're going to send us a firebase user back now it's a getter so we'll use the get keyword and we'll call this user but you can call it what you will okay so now what we want to do is return a stream inside here so we're going to return and it's built in to the firebase auth library so we're going to use this firebase auth instance first of all to grab that so underscore off and then it's dot on off and it's this one right here state changed we're returning this stream on this earth object and this stream is going to return to us firebase users whenever there's a change in authentication now that's absolutely fine but remember before we said we don't want to work with firebase users we want to work with our own custom user defined by this user model so what I'd like to do instead of getting a stream of 5/8 users every time a user signs in then I want to get a stream of normal users right so we can easily map this stream into a stream of users based on our user class by using the map method so let me come under here so there's more room for Matt and close off this file tree then I'll say dots so we're using this by the way on this on earth state change thing we're going to say dots and then map to map this to a stream of users based on I will use a class so inside this map method we can pass a function and we're going to pass in the firebase user and user that we get back to that function and inside the function all we're going to do is return user from and it's firebase user and we pass in that user so now every time we get back a firebase user inside the stream we're going to map that to a normal user based on our user class now right now we're getting a red line here because it's saying the return type is different we're not now returning a firebase use it in the stream we're returning a normal user based on our user class so we can just delete this thing right here and type user oops use it like that instead and now that red line goes away so this is absolutely fine now what we're doing here is setting up a stream so that every time a user signs in or signs out we're going to get some kind of response down this stream some kind of event to tell us look this is the current user that signed in or maybe null if the user signed out and then what we're doing is we're mapping that into our user so that in our app when we listen to this stream that user object is what we get back okay so we can grab that user object then and do something with it now there is a simpler way to do this map method right here we don't have to explicitly write out all of this function declaration what we can do is just say instead dot map by the way I'm going to comment this line out like so and we'll say dot map and all we need to do is pass in this right here so grab that and paste it in erect here and that is exactly the same this right here is exactly the same as this this functionality is implied so it passes the user that we get back down the stream into this function right here and it does the same thing so let me delete that middle line and now we have our stream set up so now when we use this we'll be setting up a stream listening forthe changes whenever a North change occurs we get back a value in the stream that value is either going to be a user object based on our user class if the earth change was the user signing in or it will be a null value if the user signs out and by the way you don't have to use a custom user class if you prefer to use the generic firebase user object I just like to abstract away from the firebase user to create a user object more suited to what I need in my app but anyway now that we've set this up in the earth service we need a way to access it in our root widget so that it can know whether a user is logged in or not and we're going to see how to do that in the next lesson

Original Description

Hey gang, in this Flutter & Firebase tutorial we'll talk about what streams are, and then set up a stream in our app to listen for auth changes. ---------------------------------------- 🐱‍💻 🐱‍💻 Course Links: Course files - https://github.com/iamshaunjp/flutter-firebase 🐱‍💻 🐱‍💻 Other Related Courses: + Flutter Tutorial for Beginners - https://www.youtube.com/playlist?list=PL4cUxeGkcC9jLYyp2Aoh6hcWuxFDX6PBJ + Firebase Auth Tutorial - https://www.youtube.com/playlist?list=PL4cUxeGkcC9jUPIes_B8vRjn1_GaplOPQ + Firebase Firestore Tutorial - https://www.youtube.com/playlist?list=PL4cUxeGkcC9itfjle0ji1xOZ2cjRGY_WB
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Net Ninja · Net Ninja · 0 of 60

← Previous Next →
1 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

Related Reads

Up next
How AI Is Transforming Analytics in Tableau Cloud & Server
Salesforce Product Center
Watch →