Skip to content

Instantly share code, notes, and snippets.

@JaveedIshaq
Created September 4, 2021 06:19
Show Gist options
  • Save JaveedIshaq/9993daf97bf45badd34398da9126a9ea to your computer and use it in GitHub Desktop.
Save JaveedIshaq/9993daf97bf45badd34398da9126a9ea to your computer and use it in GitHub Desktop.
Get Flutter FireStore Data
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
class DisplayCollection extends StatefulWidget {
@override
_DisplayCollectionState createState() => _DisplayCollectionState();
}
class _DisplayCollectionState extends State<DisplayCollection> {
final Ref = Firestore.instance.collection("Countries");
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Collection-Countries from Firebase"),
),
body: Column(
children: <Widget>[
StreamBuilder(
stream: Ref.snapshots(),
builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasData) {
return Container(
height: 600,
child: ListView.builder(
itemCount: snapshot.data.documents.length,
itemBuilder: (context, index) {
return Container(
height: 100,
child: Card(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
snapshot.data.documents[index]
.data['CountryName'],
style: TextStyle(
fontSize: 25,
fontWeight: FontWeight.bold),
),
Text(
snapshot.data.documents[index]
.data['CapitalName'],
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.w700),
),
],
),
),
);
}),
);
} else if (snapshot.hasError) {
return Text(snapshot.error.toString());
} else {
return CircularProgressIndicator();
}
},
),
],
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment