Home > OS >  Can't get around these errors in flutter cause of null safety
Can't get around these errors in flutter cause of null safety

Time:10-11

itemCount: snapShot.data.length, error - The property 'length' can't be unconditionally accessed because the receiver can be 'null'. Try making the access conditional (using '?.') or adding a null check to the target ('!').

DocumentSnapshot sliderImage = snapShot.data[index]; error -The method '[]' 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 getImage = sliderImage.data();

error - A value of type 'Object?' can't be assigned to a variable of type 'Map<dynamic, dynamic>'. Try changing the type of the variable, or casting the right-hand type to 'Map<dynamic, dynamic>'

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';

class ImageSlider extends StatefulWidget {
const ImageSlider({Key? key}) : super(key: key);

@override
_ImageSliderState createState() => _ImageSliderState();
}

class _ImageSliderState extends State<ImageSlider> {
Future getSliderImageFromDb() async {
var _fireStore = FirebaseFirestore.instance;
QuerySnapshot snapshot = await _fireStore.collection('slider').get();
return snapshot.docs;
}

@override
void initState() {
getSliderImageFromDb();
super.initState();
}

@override
Widget build(BuildContext context) {
 return Column(
  children: [
    FutureBuilder(
      initialData: [], //remove later
      future: getSliderImageFromDb(),
      builder: (_, snapshot) {
        return snapshot.data == null
            ? Container()
            : CarouselSlider.builder(
                itemCount:
                    snapshot.data!.length,
                  itemBuilder: (context, int index, int pageIndex) {
                  DocumentSnapshot sliderImage = 
                  snapshot.data[index];
                  Map getImage = sliderImage.data();
                  return Image.network(getImage['image']);
                },
                options: CarouselOptions());
      },
    )
  ],
);
}
}

CodePudding user response:

You also need to specify the FutureBuilder generic type Like let's say if you have got a list of user models from DB then it should be

 FutureBuilder<List<UserModel>>(
  initialData: [],
  future: getSliderImageFromDb(),
  builder: (_, List<UserModel> snapshot) {
    return Container();
  },
);

In Flutter2, If it is a nullable then you need to handle it, like lets say if a list is like

List<UserModel?>? users; // as this might me nullable

Then it returns a nullable UserModel, So if you want to assign this to non-nullable fields then you need to handle

final String userName = users?.elementAtIndex(3)?.userName ?? 'N.A';

Or, If you are sure that it's not a null then you can

final String userName = users!.elementAtIndex(3)!.userName;

But if its a null it throws an exception.

CodePudding user response:

snapshot.data.length

Here snapShot.data may be null so null safety is complaining.So you make sure its never null by null checking like this

if(snapshot.data==null)
 return "Something Else"

then ensure the null safety that its value will not be null ever by

snapshot.data!.length
  • Related