Created
March 7, 2022 22:17
-
-
Save romanejaquez/a430ff3cce44d984e78cbbca6b0f0b27 to your computer and use it in GitHub Desktop.
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:flutter/material.dart'; | |
import 'package:firebase_core/firebase_core.dart'; | |
import 'package:cloud_firestore/cloud_firestore.dart'; | |
const Color darkBlue = Color.fromARGB(255, 18, 32, 47); | |
void main() async { | |
WidgetsFlutterBinding.ensureInitialized(); | |
await Firebase.initializeApp( | |
options: const FirebaseOptions( | |
apiKey: "AIzaSyDOdBmVvI30Jg49oup4gsJMZMjTPhHOloQ", | |
authDomain: "fullstack-labs.firebaseapp.com", | |
databaseURL: "https://fullstack-labs.firebaseio.com", | |
projectId: "fullstack-labs", | |
storageBucket: "fullstack-labs.appspot.com", | |
messagingSenderId: "251418261584", | |
appId: "1:251418261584:web:2e01357a093d27476b7955" | |
) | |
); | |
runApp( | |
MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
debugShowCheckedModeBanner: false, | |
home: Scaffold( | |
body: Center( | |
child: Home(), | |
), | |
), | |
); | |
} | |
} | |
class Home extends StatefulWidget { | |
const Home({Key? key}) : super(key: key); | |
@override | |
_HomeState createState() => _HomeState(); | |
} | |
class _HomeState extends State<Home> { | |
static String? userID = '101'; // FirebaseAuth.instance.currentUser?.uid | |
static final userColeccion = FirebaseFirestore.instance.collection("users"); | |
var groupfav = ' '; | |
Stream<QuerySnapshot>? taskGroup; | |
@override | |
void initState() { | |
super.initState(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: const Text("Home"), | |
automaticallyImplyLeading: false, | |
), | |
body: FutureBuilder( // the future builder fetches the initial data | |
future: userColeccion.doc('$userID').get(), | |
builder: (context, snapshot) { | |
if (snapshot.hasData) { | |
var docData = (snapshot.data as DocumentSnapshot).data() as Map<String, dynamic>; | |
var groupfav = docData['groupfav']; | |
// then once the 'groupfav' has arrived, | |
// start listening | |
return Column( | |
children: [ | |
Text('Tasks for $groupfav:'), | |
Expanded( | |
child: StreamBuilder( | |
stream: FirebaseFirestore.instance | |
.collection("groups") | |
.doc(groupfav) // pass the obtained value | |
.collection("task") | |
.snapshots(), | |
builder: (context,taskSnapshot) { | |
if (taskSnapshot.hasData) { | |
var taskList = (taskSnapshot.data as QuerySnapshot<Map<String, dynamic>>). | |
docs.map((d) => Task.fromJson(d.data())).toList(); | |
return ListView.builder( | |
itemCount: taskList.length, | |
itemBuilder: (context, index) { | |
var task = taskList[index]; | |
return Container( | |
padding: const EdgeInsets.all(20), | |
child: Column( | |
children: [ | |
Text(task.titulo!, style: const TextStyle(color: Colors.blue, fontSize: 30)), | |
Text(task.contenido!) | |
] | |
) | |
); | |
} | |
); | |
} | |
return const Center( | |
child: CircularProgressIndicator() | |
); | |
} | |
), | |
) | |
] | |
); | |
} | |
return const Center( | |
child: CircularProgressIndicator() | |
); | |
} | |
) | |
); | |
} | |
} | |
class Task { | |
String? titulo; | |
String? contenido; | |
Task({ this.titulo, this.contenido }); | |
factory Task.fromJson(Map<String, dynamic> json) { | |
return Task( | |
titulo: json['titulo'], | |
contenido: json['contenido'] | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment