Toggle Buttons, failed assertion line 188 pos 12: 'isSelected != null is not true

Viewed 690

I am currently learning Flutter and I try to make a text ToogleButtons, But I got this error :

'package:flutter/src/material/toogle_buttons.dart': Failed assertion: line 188 pos 12: 'isSelected != null': is not true.

I've changed the bool of list isSelected to [true, false, false]; but it didn't work,

My code as follow :


  List<bool> isSelected = [false, false, false];
  FocusNode focusNodeButton1 = FocusNode();
  FocusNode focusNodeButton2 = FocusNode();
  FocusNode focusNodeButton3 = FocusNode();
  List<FocusNode> focusToggle;

  @override
  void initState() {
    // ignore: todo
    // TODO: implement initState
    super.initState();
    focusToggle = [focusNodeButton1, focusNodeButton2, focusNodeButton3];
  }

  @override
  void dispose() {
    // Clean up the focus node when the Form is disposed.
    focusNodeButton1.dispose();
    focusNodeButton2.dispose();
    focusNodeButton3.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: <Widget>[
        Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.end,
          children: <Widget>[
            Padding(
              padding: const EdgeInsets.only(left: kDefaultPadding / 2),
              child: ToggleButtons(
                borderColor: Colors.black,
                fillColor: Colors.white,
                borderWidth: 2,
                borderRadius: BorderRadius.circular(2),
                selectedBorderColor: kMaincolor,
                selectedColor: kMaincolor,
                focusNodes: focusToggle,
                children: <Widget>[
                  Padding(
                    padding: EdgeInsets.all(5),
                    child: Text(
                      "Lingkar Teman",
                      style: TextStyle(fontSize: 15),
                    ),
                  ),
                  Padding(
                    padding: EdgeInsets.all(5),
                    child: Text(
                      "Orang Lain",
                      style: TextStyle(fontSize: 15),
                    ),
                  ),
                  Padding(
                    padding: EdgeInsets.all(5),
                    child: Text(
                      "Semua",
                      style: TextStyle(fontSize: 15),
                    ),
                  ),
                ],
                isSelected: isSelected,
                onPressed: (int index) {
                  setState(() {
                    isSelected[index] = !isSelected[index];
                  });
                },
              ),
            ),
          ],
        ),
1 Answers

The code looks fine to me. The possible issue can be you wrote isSelected list after running the app and now you are trying to hot reload the app so it's taking it as null and this issue is happening. Please try to restart the app or initialize the isSelected in build. Moreover you cannot revert the state of array just like that as you need to check the index. You can replace setState() content as the following

setState(() {
    for (int i = 0; i < isSelected.length; i++) {
        isSelected[i] = i == index
    }
});
Related