Skip to content

Instantly share code, notes, and snippets.

@ProjectInitiative
Last active March 25, 2019 19:27
Show Gist options
  • Save ProjectInitiative/4444f5ceeeb8f3d419170f6d75f2a56e to your computer and use it in GitHub Desktop.
Save ProjectInitiative/4444f5ceeeb8f3d419170f6d75f2a56e to your computer and use it in GitHub Desktop.
A quick example on how to implement a StreamBuilder that requires a Future<String> within the stream: attribute
FutureBuilder(
future: _getUID(),
builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
if (snapshot.connectionState != ConnectionState.done)
return new Center(
child: new Container(child: CircularProgressIndicator()));
return new StreamBuilder(
stream: _getStream(snapshot.data.toString()),
builder: (BuildContext context, userSnapshot) {
// TEMPORARY FIX
// The error lies in trying to access a key 'email' before it is created
try{
if (userSnapshot.hasData && userSnapshot.data != null)
return new Center(
child: new Container(
child: Text("${userSnapshot.data['email']}",
style: TextStyle(fontSize: 20))));
return new Center(
child: new Container(
child: CircularProgressIndicator()));
}
catch (Exception){
return new Center(
child: new Container(
child: CircularProgressIndicator()));
}
});
return new Center(
child: new Container(
child: CircularProgressIndicator()));
});
Future<String> _getUID() async {
FirebaseUser user = await FirebaseAuth.instance
.currentUser();
// or you can use condense to one line
//return FirebaseAuth.instance.currentUser().then((FirebaseUser user) => user.uid);
return user.uid;
}
Stream<DocumentSnapshot> _getStream(String uid) {
return Firestore.instance.collection('users').document(uid).snapshots();
}

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.

In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to http://unlicense.org

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment