I am new to Flutter as well as Android, currently I am doing Search Data from Firestore. I was able to get the data but I couldn't display it on the screen. I'm getting this error
"The method '[]' can't be unconditionally invoked because the receiver can be 'null'."
_querySnapshot.docs[index].data()['name']
And this is the code I'm using for searching
late QuerySnapshot _querySnapshot;
@override
Widget build(BuildContext context) {
Widget searchedData() {
return ListView.builder(
itemCount: _querySnapshot.docs.length,
itemBuilder: (BuildContext context, int index) {
return ListTile(
title: Text(
_querySnapshot.docs[index].data()['name'],
//
),
);
});
}
return LayOut(
Scaffold(
floatingActionButton: FloatingActionButton(
child: Icon(Icons.clear), onPressed: emptyTextSearch),
appBar: AppBar(
actions: [
GetBuilder<DataController>(
init: DataController(),
builder: (val) {
return IconButton(
icon: Icon(Icons.search),
onPressed: () {
val.queryData(_searchController.text).then((value) {
_querySnapshot = value;
setState(() {
isExcecuted = true;
});
});
});
})
],
title: TextField(
style: TextStyle(color: Colors.white),
decoration: InputDecoration(
hintText: 'Search', hintStyle: TextStyle(color: Colors.white)),
controller: _searchController,
),
backgroundColor: Colors.black,
),
body: isExcecuted
? searchedData()
: Container(
child: Center(
child: Text(
'Search any',
style: TextStyle(color: Colors.white, fontSize: 30.0),
),
),
),
),
);
}
DataController
class DataController extends GetxController {
Future getData(String collection) async {
final FirebaseFirestore firebaseFirestore = FirebaseFirestore.instance;
QuerySnapshot snapshot =
await firebaseFirestore.collection(collection).get();
return snapshot.docs;
}
Future queryData(String queryString) async {
return FirebaseFirestore.instance
.collection('reports')
.where('property', isGreaterThanOrEqualTo: queryString)
.get();
}
}
Please help me get past this error.