import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../components/grocery_item_tile.dart';
import '../model/cart_model.dart';
import 'cart_page.dart';
class HomePage extends StatefulWidget {
const HomePage({super.key});
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
final _firestore = FirebaseFirestore.instance;
@override
Widget build(BuildContext context) {
CollectionReference damacanaRef = _firestore.collection("damacana");
var kaylaRef = damacanaRef.doc("KaylaSu");
/**
StreamBuilder<DocumentSnapshot>(
stream: damacanaRef.snapshots(),
builder: (context,AsyncSnapshot asyncSnapshot){
return Scaffold();
},
)
**/
return StreamBuilder<QuerySnapshot>(
stream: damacanaRef.snapshots(),
builder: (BuildContext contex, AsyncSnapshot asyncSnapshot){
/// List<DocumentSnapshot> listOfDocSnap = asyncSnapshot.data.docs;
/// List<DocumentSnapshot> listOfDocSnap = asyncSnapshot.data!.docs.map((doc) => doc as DocumentSnapshot).toList();
if(asyncSnapshot.hasError){
return Center(child: Text("Bir Hata ile Karşılaşıldı. Sonra Tekrar Deneyiniz."));
}else{
if(asyncSnapshot.hasData){
List<DocumentSnapshot> listOfDocSnap = asyncSnapshot.data.docs;
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.transparent,
elevation: 0,
leading: Padding(
padding: const EdgeInsets.only(left: 24.0),
child: Icon(
Icons.location_on,
color: Colors.grey[700],
),
),
title: Text(
'Istanbul, Cekmekoy ',
style: TextStyle(
fontSize: 16,
color: Colors.grey[700],
),
),
centerTitle: false,
actions: [
Padding(
padding: const EdgeInsets.only(right: 24.0),
child: Container(
padding: EdgeInsets.all(16),
decoration: BoxDecoration(
color: Colors.grey[200],
borderRadius: BorderRadius.circular(12),
),
child: Icon(
Icons.person,
color: Colors.grey,
),
),
),
],
),
floatingActionButton: FloatingActionButton(
backgroundColor: Colors.black,
onPressed: () =>
Navigator.push(
context,
MaterialPageRoute(
builder: (context) {
return CartPage();
},
),
),
child: const Icon(Icons.shopping_bag),
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const SizedBox(height: 48),
// good morning bro
const Padding(
padding: EdgeInsets.symmetric(horizontal: 24.0),
child: Text('Merhaba,'),
),
const SizedBox(height: 4),
// Let's order fresh items for you
const Padding(
padding: EdgeInsets.symmetric(horizontal: 24.0),
child: Text(
"Sizin için ürünlerimiz",
style: TextStyle(
fontSize: 36,
fontWeight: FontWeight.bold,
),
),
),
const SizedBox(height: 24),
const Padding(
padding: EdgeInsets.symmetric(horizontal: 24.0),
child: Divider(),
),
const SizedBox(height: 24),
// categories -> horizontal listview
Padding(
padding: EdgeInsets.symmetric(horizontal: 24.0),
child: Text(
"${listOfDocSnap.first}",
style: TextStyle(
//fontWeight: FontWeight.bold,
fontSize: 18,
),
),
),
// recent orders -> show last 3
Expanded(
child: Consumer<CartModel>(
builder: (context, value, child) {
return GridView.builder(
padding: const EdgeInsets.all(12),
physics: const NeverScrollableScrollPhysics(),
itemCount: value.shopItems.length,
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: 1 / 1.2,
),
itemBuilder: (context, index) {
return GroceryItemTile(
itemName: value.shopItems[index][0],
itemPrice: value.shopItems[index][1],
imagePath: value.shopItems[index][2],
color: value.shopItems[index][3],
onPressed: () =>
Provider.of<CartModel>(context, listen: false)
.addItemToCart(index),
);
},
);
},
),
),
],
),
);
}else{
return Center(child: CircularProgressIndicator(),);
}
}
},
);
}
}
I am making a market application. I want to pull data from firebase and reflect it directly to the screen. it’s pulling from the list right now I’m trying to pull data from firebase. code like this. The "Text" message in the async.error structure is always reflected on the screen. How can I solve the error?
>Solution :
The error message "Bir Hata ile Karşılaşıldı. Sonra Tekrar Deneyiniz." (An error occurred. Please try again later.) is displayed when there’s an error in the StreamBuilder snapshot. To solve this error and display the actual error message, you can update your code as follows:
Replace the line:
return Center(child: Text("Bir Hata ile Karşılaşıldı. Sonra Tekrar Deneyiniz."));
with:
return Center(child: Text("Error: ${asyncSnapshot.error}"));
This change will display the actual error message on the screen, which can help you identify and resolve the issue.