I have a problem. It is getting data from firebase realtime database. If data is added it throws an error.
_TypeError (type 'String' is not a subtype of type 'int' of 'index')
According to the image below that has been flattened
realtime databse Json in my code patient_ID is "1264758392012"
{
"notification": {
"1264758392012": {
"-NWD2baVxumoarhIVQS6": {
"calling_by": "Nurse",
"is_read": true,
"misscall": true,
"timestamp": "2023-05-24 20:59:11.535176"
},
"-NWD2rEc8eGGaOF72fCF": {
"calling_by": "Nurse",
"is_read": false,
"misscall": true,
"timestamp": "2023-05-24 21:00:15.649757"
},
"-NWD3tqlVO5TDmOuHOoR": {
"calling_by": "Nurse",
"is_read": false,
"misscall": true,
"timestamp": "2023-05-24 21:04:48.450108"
},
}
}
}
realtime database
void realtime()async {
final ref = await FirebaseDatabase.instance.ref();
ref.child('notification/$patient_ID').onValue.listen((event) {
final snapshot = event.snapshot;
if (snapshot.exists) {
setState(() {
// dataNotification.clear();
data = snapshot.value as Map<dynamic, dynamic>;
});
setState(() {
dataNotification.clear();
dataNotification.addAll(data.values);
});
print("dataNoti: $dataNotification");
} else {
print('No data available.');
}
});
}
Listview.builder
ListView.builder(
key: UniqueKey(),
physics: NeverScrollableScrollPhysics(),
padding: EdgeInsets.zero,
shrinkWrap: true,
itemCount: dataNotification.length,
itemBuilder: (BuildContext context,int index) {
final itemDataNoti = dataNotification[index];
return Container(
height: 80,
width: double.infinity,
child: Card(
child: Padding(
padding: const EdgeInsets.all(10.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.only(left: 10),
child: Icon(
Icons.phone_missed,
color: Colors.red,
),
),
Padding(
padding: const EdgeInsets.only(left: 20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
itemDataNoti == null ?Text("data") :Text(
"calling by: ${itemDataNoti["calling_by"].toString()}",
style: GoogleFonts.notoSansThai(),
),
],
),
),
],
),
),
),
);
},
)