flutter on click download image from firebase - firebase

Here, when I add a new picture, it appears here and it can be opened to a full screen, but when I click I want to download the picture that I long pressed on to the phone storage
StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance.collection('gallery').snapshots(),
builder: (context, snapshot) {
return !snapshot.hasData ? Center(
child: CircularProgressIndicator(),
) : Container(
child: GridView.builder(
itemCount: snapshot.data!.docs.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3
),
itemBuilder: (context, index) {
return Container(
margin: EdgeInsets.all(3),
child: FullScreenWidget(
backgroundColor: Colors.white,
child: FadeInImage.assetNetwork(
fit: BoxFit.contain,
placeholder: 'assets/loading.gif',
imageErrorBuilder:
(context, error, stackTrace) {
return Image.asset(
'assets/error.gif',
fit: BoxFit.fitWidth);
},
image: snapshot.data!.docs[index].get('url')),
),
);
}),
);
},
);

Related

Pass post details to new page (Firebase in Flutter)

Multi level Flutter Page
I need to add the functionality of pressing a post card and it loading a more detailed page with that posts information. So far I have developed the listview displaying the post cards and have wrapped them with an InkWell that pushes to the detailed page.
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Friends')),
body: FutureBuilder(
future: FirebaseFirestore.instance
.collection('posts')
.where('uid', isEqualTo: widget.uid)
.get(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(
child: CircularProgressIndicator(),
);
}
return ListView.builder(
shrinkWrap: true,
physics: const ScrollPhysics(),
itemCount: (snapshot.data! as dynamic).docs.length,
itemBuilder: (context, index) {
DocumentSnapshot snap =
(snapshot.data! as dynamic).docs[index];
return Container(
decoration: BoxDecoration(
color: Colors.grey.shade300,
borderRadius:
BorderRadius.all(Radius.circular(20))),
padding: const EdgeInsets.all(8.0),
margin: const EdgeInsets.all(8.0),
child: Row(
children: [
InkWell(
child: Container(
padding: EdgeInsets.all(10),
width: 140,
height: 140,
child: ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Container(
child: Material(
child: Ink.image(
image:
NetworkImage(snap['postUrl']),
fit: BoxFit.cover,
child: InkWell(
onTap: () => Navigator.push(
context,
MaterialPageRoute(
builder: (context,) =>
DetailedPage(uid: FirebaseAuth.instance.currentUser!.uid)),
),
)),
))),
),
),

Flutter + Firebase : How to view images depend on id?

I have an app show data from firebase ,and image in folder called category in firebase storage ,the name of image is same id field of collections.
now I want to view data and view image of same field :
child: ListView.builder(
padding: EdgeInsets.all(10),
itemCount: doctorData.length,
itemBuilder: (context, index) {
return Card(
elevation: 15,
child: ListTile(
leading: Container(
width: 60,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(30)),
child: Image(
image: NetworkImage(
'gs://myproject.appspot.com/categories/categories/${doctorData[index]['id']}.png'),
)
),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => Detail(doctorData[index]),
),
);
},
title: Text('Dr. ${doctorData[index]['name_en']}',
style: TextStyle(fontSize: 22)),
subtitle: Text('د. ${doctorData[index]['name_ar']}',
style: TextStyle(fontSize: 22)),
),
);
}),
),
The image isn't shown , How can I view the image
I had the same issue, but after a while i figured this out. I used Futurebuilder and CachedNetworkImageProvider and they are solved my problem.
If you want to you the CachedNetworkImageProvider you need this in the pubspec.yaml.
dependencies:
cached_network_image: ^2.3.3
FutureBuilder<dynamic>(
future: FirebaseStorage().ref('yourImage.jpg').getDownloadURL(),
builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
if (snapshot.connectionState != ConnectionState.waiting) {
return Image(
image: CachedNetworkImageProvider(snapshot.data.toString()),
fit: BoxFit.cover,
);
} else {
return Text('Loading image....');
}
},
),

Horizontal ListView inside SliverAppBar

I am trying to add a Horizontal ListView.builder Inside a SliverAppBar, but every list item appears on top of each other like a traditional list (VERTICLE), when try to change the Scroll direction the whole list disappear.
this is the Items inside SAB
body: SingleChildScrollView(
child: Column(
children: [
CATS(),
Center(
child: Text('خانه های کرایی جدید'),
SizedBox(height: 5),
LatestForRent(),//TO BE SCROLLED HORIZONTAL
SizedBox(height: 5),
Center(
child: Text('خانه های فروشی جدید'),
SizedBox(height: 5),
LatestForSale(), //TO BE SCROLLED HORIZONTAL
SizedBox(height: 5),
Center(
child: Text('خانه های گروی جدید'),
SizedBox(height: 5),
LatestGerawi(),//TO BE SCROLLED HORIZONTAL
this is the List
StreamBuilder(
stream: FirebaseFirestore.instance
.collection('Sell')
.orderBy('TS', descending: true)
.snapshots(),
builder: (context, snapshot) {
if (snapshot.data == null)
return Center(
child: CircularProgressIndicator(
backgroundColor: Colors.red,
valueColor: new AlwaysStoppedAnimation<Color>(Colors.cyan[400]),
),
);
return ListView.builder(
// scrollDirection: Axis.horizontal,
shrinkWrap: true,
itemCount: 2,
itemBuilder: (context, index) => Padding(
padding: const EdgeInsets.all(5.0),
child: Container(
height: 200,
width: 300,
Give an height to your ListView.
StreamBuilder(
stream: FirebaseFirestore.instance
.collection('Sell')
.orderBy('TS', descending: true)
.snapshots(),
builder: (context, snapshot) {
if (snapshot.data == null)
return Center(
child: CircularProgressIndicator(
backgroundColor: Colors.red,
valueColor: new AlwaysStoppedAnimation<Color>(Colors.cyan[400]),
),
);
return Container(
height: 200,
child:ListView.builder(
scrollDirection: Axis.horizontal,
shrinkWrap: true,
itemCount: 2,
itemBuilder: (context, index) => Padding(
padding: const EdgeInsets.all(5.0),
child: Container(
height: 200,
width: 300,
)));

Flutter Firebase failed assertion: line 360 pos 10: 'data != null'

Widget BlogsList() {
return Container(
color: UniversalVariables.blackColor,
child: blogsStream != null
? Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
StreamBuilder(
stream: blogsStream,
builder: (context, snapshot) {
if (!snapshot.hasData || snapshot.data.documents.isEmpty) ;
return ListView.builder(
padding: EdgeInsets.symmetric(horizontal: 16),
itemCount: snapshot.data.documents.length,
shrinkWrap: true,
itemBuilder: (context, index) {
return BlogsTile(
authorName: snapshot
.data.documents[index].data['authorName'],
title: snapshot.data.documents[index].data["title"],
description:
snapshot.data.documents[index].data['desc'],
imgUrl:
snapshot.data.documents[index].data['imgUrl'],
);
});
},
)
],
)
: Container(
alignment: Alignment.center,
child: CircularProgressIndicator(),
),
); }
I want to make it mandatory. giving a red screen and open.I want to solve this problem. I am pulling data with Firebase. I'm having trouble switching between pages, but could not solve my error.
The error occurred is in your builder, you returned the ListView whether snapshot.hasData is true or not.
Try
builder: (context, snapshot) {
if (!snapshot.hasData || snapshot.data.documents.isEmpty){
return Center(child: Text("Loadin..."));
}
return ListView.builder(
padding: EdgeInsets.symmetric(horizontal: 16),
itemCount: snapshot.data.documents.length,
shrinkWrap: true,
itemBuilder: (context, index) {
return BlogsTile(
authorName: snapshot.data.documents[index].data['authorName'],
title: snapshot.data.documents[index].data["title"],
description:snapshot.data.documents[index].data['desc'],
imgUrl:snapshot.data.documents[index].data['imgUrl'],
);
});
},

Flutter: Do FutureBuilder Loads everything at the same time?

My app has a book summaries Feature, Now since I don't have money to host an API, I am using Firebase/Firestore database where I add summaries manually and then retrieve data from firebase to App.
I am using FutureBuilder for it.
Now say I have 10 summaries will FutureBuilder first load all 10 of them and then display data on screen(which is a ListView.builder and can show only 2 summaries without scrolling) or it will load only the data which need to be painted on the screen just like simple ListView.builder.
// code to retrieve data:
Future<QuerySnapshot> getFeedsfromFb() async {
var data = await Firestore.instance
.collection('feedItem')
.orderBy('feedId', descending: true).getDocuments();
return data;
}
// building widget:
Widget feed() {
return Container(
width: deviceWidth,
height: deviceHeight / 3,
child: FutureBuilder(
future: getFeedsfromFb(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasData) {
return ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: snapshot.data.documents.length > 10
? 10
: snapshot.data.documents.length,
itemBuilder: (BuildContext context, int index) {
return Container(
width: deviceWidth / 2.5,
child: GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (BuildContext context) => FeedIntro(
snapshot.data.documents[index]['feedId'])));
},
child: Card(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Container(
// width: 150,
height: 150,
foregroundDecoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage(
snapshot.data.documents[index]['feedImage'],
),
fit: BoxFit.fill)),
),
Center(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(snapshot.data.documents[index]['title']),
)),
],
)),
),
);
},
);
} else if (snapshot.hasError) {
return Center(child: Text('Sorry Something went wrong!'));
} else {
return Center(
child: SizedBox(
child: CircularProgressIndicator(),
width: 50,
height: 50,
),
);
}
}),
);
}
Your FutureBuilder will load all items at the same, but only needed data by ListView.builder will be painted on the screen.

Resources