Rex
Rex

Reputation: 313

A value of type 'bool?' can't be assigned to a variable of type 'bool'

Because of function having bool statement i have used Function(bool?) here

Widget buildSwitchtile(String title, String desc, bool currentvalue,
          Function(bool?) updatevalue) {
        return SwitchListTile(
          title: Text(title),
          value: currentvalue,
          subtitle: Text(desc),
          onChanged: updatevalue,
        );
      }

the following error occured how to solve this ? A value of type 'bool?' can't be assigned to a variable of type 'bool'. Please help me out in this.-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

enter image description here

import 'package:dishes_app/widgets/main_drawer.dart';
import 'package:flutter/material.dart';

class FilterScreen extends StatefulWidget {
  static const routeName = '/filters';

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

class _FilterScreenState extends State<FilterScreen> {
  bool _glutenFree = false;
  bool _vegetarian = false;
  bool _vegan = false;
  bool _lactoseFree = false;

  Widget buildSwitchtile(String title, String desc, bool currentvalue,
      Function(bool?) updatevalue) {
    return SwitchListTile(
      title: Text(title),
      value: currentvalue,
      subtitle: Text(desc),
      onChanged: updatevalue,
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Settings'),
      ),
      drawer: MainDrawer(),
      body: Column(
        children: [
          Container(
            padding: EdgeInsets.all(20.0),
            child: Text(
              'Adjust Your meal selection',
              style: Theme.of(context).textTheme.title,
            ),
          ),
          Expanded(
            child: ListView(
              children: [
                buildSwitchtile('Gluten-Free', 'Only include gluten-free meals',
                    _glutenFree, (newvalue) {
                  setState(() {
                    _glutenFree = newvalue;
                  });
                }),
                buildSwitchtile(
                    'Lactose-Free', 'Only include Lactose Free', _lactoseFree,
                    (newvalue) {
                  setState(() {
                    _lactoseFree = newvalue;
                  });
                }),
                buildSwitchtile('Vegetarian ', 'Only include Vegetarian food  ',
                    _vegetarian, (newvalue) {
                  setState(() {
                    _vegetarian = newvalue;
                  });
                }),
                buildSwitchtile('Vegan', 'Only include Vegan', _vegan,
                    (newvalue) {
                  setState(() {
                    _vegan = newvalue;
                  });
                })
              ],
            ),
          ),
        ],
      ),
    );
  }
}

Upvotes: 0

Views: 884

Answers (3)

Mirza Talha
Mirza Talha

Reputation: 11

You don't have to do much just add bool in function instead of Function(bool?) updatevalue use this Function(bool) updatevalue

Upvotes: 0

nvoigt
nvoigt

Reputation: 77304

The onChanged method can only emit a bool. So do that. Pass a method that takes a bool. Then you should not have the problem you are having.

  Widget buildSwitchtile(String title, String desc, bool currentvalue,
      Function(bool) updatevalue) {
    return SwitchListTile(
      title: Text(title),
      value: currentvalue,
      subtitle: Text(desc),
      onChanged: updatevalue,
    );
  }

Upvotes: 0

Rohan Patil
Rohan Patil

Reputation: 317

Put "!" in the end when using that bool. It(bool? myBool) has a nullable value so when using it,you have to do myBool!

A quick guide https://youtu.be/Q_WloMNKOkU

Upvotes: 3

Related Questions