firebase firestore fetching data
the below code im getting like below and the screen is just loading:
ConnectionState.waiting
I/flutter (13393): StreamBuilder Waiting for data...
I/flutter (13393): Building with Device IDs: [12345678]
I/flutter (13393): StreamBuilder Connection State: ConnectionState.waiting
I/flutter (13393): StreamBuilder Waiting for data...
I/flutter (13393): Building with Device IDs: [12345678]
I/flutter (13393): StreamBuilder Connection State: ConnectionState.waiting
I/flutter (13393): StreamBuilder Waiting for data...
I/flutter (13393): Building with Device IDs: [12345678]
I/flutter (13393): StreamBuilder Connection State: ConnectionState.waiting
I/flutter (13393): StreamBuilder Waiting for data...
class ShowDevices extends ConsumerStatefulWidget {
const ShowDevices({Key? key}) : super(key: key);
@override
_ShowDevicesState createState() => _ShowDevicesState();
}
class _ShowDevicesState extends ConsumerState<ShowDevices> {
DeviceModel? deviceModel;
Map<String, dynamic>? deviceData;
void button() {
List<String> devicesIds = ref.watch(userProvider)!.devices;
print('Using device ID: ${devicesIds.first}');
}
@override
Widget build(BuildContext context) {
List<String> devicesIds = ref.watch(userProvider)!.devices;
print('Building with Device IDs: $devicesIds');
return Scaffold(
appBar: AppBar(
title: Text('Firestore Data'),
),
body: Column(
children: [
TextButton(
onPressed: () {
button();
},
child: Text('button'),
),
StreamBuilder<DocumentSnapshot>(
stream: ref
.read(fireStoreProvider)
.collection("devices")
.doc(devicesIds.isNotEmpty ? devicesIds.first : '123')
.snapshots(),
builder: (context, snapshot) {
print(
'StreamBuilder Connection State: ${snapshot.connectionState}');
if (snapshot.hasError) {
print('StreamBuilder Error: ${snapshot.error}');
return Center(
child: Text(
'Error retrieving data: ${snapshot.error}',
style: TextStyle(
color: Colors.red,
fontSize: 15,
),
),
);
}
if (snapshot.connectionState == ConnectionState.waiting) {
print('StreamBuilder Waiting for data...');
return Center(
child: CircularProgressIndicator(),
);
}
if (!snapshot.hasData || snapshot.data!.data() == null) {
print('StreamBuilder No data available');
return Center(
child: Text('No data available'),
);
}
deviceData = snapshot.data!.data()! as Map<String, dynamic>;
deviceModel = DeviceModel.fromMap(deviceData!);
return Column(
children: [
TextButton(onPressed: () {}, child: Text('save')),
Text(deviceModel!.name),
],
);
},
),
],
),
);
}
}
if I don't use deviceIDs List and use directly '12345678' im getting the data but if i use like above code with deviceIds list I'm just seeing loading.
with below I'm getting proper data but I want to use a list and get list of devices to display:
class ShowDevices extends ConsumerStatefulWidget {
const ShowDevices({Key? key}) : super(key: key);
u/override
_ShowDevicesState createState() => _ShowDevicesState();
}
class _ShowDevicesState extends ConsumerState<ShowDevices> {
DeviceModel? deviceModel;
Map<String, dynamic>? deviceData;
u/override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Firestore Data'),
),
body: StreamBuilder<DocumentSnapshot>(
stream: ref
.read(fireStoreProvider)
.collection("devices")
.doc('12345678')
.snapshots(),
builder: (context, snapshot) {
if (snapshot.hasError) {
return Center(
child: Text(
'Error retrieving data: ${snapshot.error}',
style: TextStyle(
color: Colors.red,
fontSize: 15,
),
),
);
}
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(
child: CircularProgressIndicator(),
);
}
if (!snapshot.hasData || snapshot.data!.data() == null) {
return Center(
child: Text('No data available'),
);
}
deviceData = snapshot.data!.data()! as Map<String, dynamic>;
deviceModel = DeviceModel.fromMap(deviceData!);
return Column(
children: [
TextButton(onPressed: () {}, child: Text('save')),
Text(deviceModel!.name),
// Add other widgets to display device data as needed
],
);
},
),
);
}
}
please help me I'm stuck with this from 2 days I'm a beginner.
Posted Mar 28, 2024, 10:16 AM UTC