Search code examples
flutterscoped-model

Failed assertion: boolean expression must not be null with scoped model on flutter


I'm trying to create a function using scoped model which there will be a favorite_border icon and when it's pressed changes to a favorite icon. Besides that an increment counter will show the number of likes from the firebase data to the viewer. I'm trying to use scoped model to make this function but I'm getting the error "Failed assertion: boolean expression must not be null". Any Ideas on the issue?

class LikesModel extends Model {

DocumentSnapshot snapshot;

bool liked = false;

static LikesModel of(BuildContext context) =>
  ScopedModel.of<LikesModel>(context);


bool isLiked() {

 liked = true;
 notifyListeners();

}

void pressed(){
liked = !liked;
notifyListeners();
}

void changeLikes() {
Firestore.instance
    .collection(snapshot.documentID)
    .document(snapshot.documentID)
    .updateData({'likes': FieldValue.increment(liked ? 1 : -1)});
notifyListeners();
}
}

class LanchonetesContact extends StatefulWidget {

final DocumentSnapshot lanchonetes;

LanchonetesContact(this.lanchonetes);

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

class _LanchonetesContactState extends State<LanchonetesContact> {


@override
Widget build(BuildContext context) {
return Padding(
              padding: EdgeInsets.only(top: 0.0),
              child: Card(
                  elevation: 1.0,
                  child: GestureDetector(
                      child: Container(
                        height: 70.0,
                        width: 390.0,
                        color: Colors.white,
                        child: Row(
                          children: <Widget>[
                            Icon(
                              LikesModel.of(context).isLiked() ?       
                                Icons.favorite : Icons.favorite_border,
                              color: LikesModel.of(context).isLiked() ?   
                              Colors.red : Colors.black,
                              size: 50.0,
                            ),

                            StreamBuilder(
                               stream: Firestore.instance
                               .collection('lanchonetes')
                               .document(widget.lanchonetes.documentID)
                               .snapshots(),
                               builder: (context, snapshot) => Text(
                               snapshot.data.data["likes"].toString(),
                                  style: TextStyle(fontSize: 40.0),
                                ) 
                            ),
                          ],
                          mainAxisAlignment: MainAxisAlignment.center,
                        ),
                      ),
                      onTap: () {
                        LikesModel.of(context).changeLikes();
                        LikesModel.of(context).pressed();

                      }

                  ))


          ),

Solution

  • I don't understand completely the goal of isLiked(), but it is not returning anything and is expected a bool to be returned

    bool isLiked() {
    
     liked = true;
     notifyListeners();
    
    }
    

    This throws an error here in the code below, as LikesModel.of(context).isLiked() returns null and a bool in a conditional operator (?:) cannot be null

    Icon(
       LikesModel.of(context).isLiked() ?       
        Icons.favorite : Icons.favorite_border,
        color: LikesModel.of(context).isLiked() ?   
          Colors.red : Colors.black,
        size: 50.0,
    ),
    

    If you just want to check liked you should just do

    bool isLiked() => liked;

    or even cleaner

    bool get isLiked => liked; //Probably it would be nicer to make liked private :_liked.