【问题标题】:Toggle Buttons, failed assertion line 188 pos 12: 'isSelected != null is not true切换按钮,断言行 188 pos 12 失败:'isSelected != null is not true
【发布时间】:2020-11-24 09:55:35
【问题描述】:

我目前正在学习 Flutter,我尝试制作一个文本 ToogleButtons,但我收到了这个错误:

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

我已将列表 isSelected 的布尔值更改为 [true, false, false];,但它不起作用,

我的代码如下:


  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];
                  });
                },
              ),
            ),
          ],
        ),

【问题讨论】:

    标签: flutter dart togglebutton


    【解决方案1】:

    代码对我来说看起来不错。可能的问题是您在运行应用程序后写了isSelected 列表,现在您正在尝试热重新加载应用程序,因此它将其视为null,并且正在发生此问题。请尝试重新启动应用程序或在构建中初始化isSelected。此外,您不能像那样恢复数组的状态,因为您需要检查索引。您可以将setState()内容替换为如下

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

    【讨论】:

      猜你喜欢
      • 2021-03-07
      • 2020-06-05
      • 1970-01-01
      • 1970-01-01
      • 2020-11-30
      • 1970-01-01
      • 2023-01-24
      • 2018-12-28
      • 1970-01-01
      相关资源
      最近更新 更多