this is the code can any help me how to change this to null safety, I am getting errors in .data(). i changed my project to null-safety, and then i am facing this issue
this is the error
The method 'data' can't be unconditionally invoked because the receiver can be 'null'. Try making the call conditional (using '?.') or adding a null check to the target ('!').
Map<String, dynamic>? documentData = snapshot.data.data();
class HomeSlider extends StatefulWidget {
final String? doc_id;
HomeSlider({this.doc_id});
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<HomeSlider> {
FirebaseServices _firebaseServices = FirebaseServices();
int activeIndex = 1;
@override
Widget build(BuildContext context) {
super.build(context);
return Container(
// height: 200,
child: FutureBuilder(
future: _firebaseServices.sliderRef
.doc(widget.doc_id == null ? "Slider" : widget.doc_id)
.get(),
builder: (context, snapshot) {
if (snapshot.hasError) {
return Center(
child: Text("Error: ${snapshot.error}"),
);
}
if (snapshot.connectionState == ConnectionState.done) {
// the error is here in data()
// Firebase Document Data Map
Map<String, dynamic> documentData = snapshot.data.data();
List? imageList = documentData['images'];
List? suid = documentData['suid'];
return SliderBody(
imageList: imageList,
suid: suid,
);
}
return Center(
child: CircularProgressIndicator(),
);
}));
}}
CodePudding user response:
you should go with
Map<String, dynamic>? documentData = snapshot!.data.data();
CodePudding user response:
the problem was in builder: (context, snapshot) {
after adding the AsyncSnapshot
and finally like this. builder: (context, AsyncSnapshot snapshot) {
and also add !
before .data()
class HomeSlider extends StatefulWidget {
final String? doc_id;
HomeSlider({this.doc_id});
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<HomeSlider> {
FirebaseServices _firebaseServices = FirebaseServices();
int activeIndex = 1;
@override
Widget build(BuildContext context) {
super.build(context);
return Container(
// height: 200,
child: FutureBuilder(
future: _firebaseServices.sliderRef
.doc(widget.doc_id == null ? "Slider" : widget.doc_id)
.get(),
builder: (context,AsyncSnapshot snapshot) {
if (snapshot.hasError) {
return Center(
child: Text("Error: ${snapshot.error}"),
);
}
if (snapshot.connectionState == ConnectionState.done) {
// the error is here in data()
// Firebase Document Data Map
Map<String, dynamic> documentData = snapshot.data!.data();
List? imageList = documentData['images'];
List? suid = documentData['suid'];
return SliderBody(
imageList: imageList,
suid: suid,
);
}
return Center(
child: CircularProgressIndicator(),
);
}));
}}```
Please upvote my answers if worked for you