dart 尝试从TexteditingController获取整数以构建M

9wbgstp7  于 2023-02-20  发布在  其他
关注(0)|答案(1)|浏览(80)

我想做一个匀光(乘法)表...并希望从TexteditingController中获取用户的输入,但我无法从文本编辑控制器中获取整数值。期待是否有人会调查它,

TextEditingController num = TextEditingController();
  void table() {
    print('enter the number $num');

    for (var i = 1; i <= 10; i++) {
      print('$num*$i = ${num * i}'); //<error>(The operator '*' isn't defined for the type,,'')
    }
  }
    
  Scaffold(
    body: Column(
      children: [
        TextFormField(
          controller: TextEditingController(),
        ),
        Container(
          height: 400,
          width: 400,
          decoration: BoxDecoration(
            color: Colors.black,
            borderRadius: BorderRadius.circular(20),
          ),
          child: ElevatedButton(
            onPressed: table, 
            child: Text('')),
          ),
        ],
      ));
    }
  }
tzdcorbm

tzdcorbm1#

该错误源于您直接访问TextEditingController而不是获取它所包含的文本。
正确答案是:

TextEditingController controller = TextEditingController();
void table() {
  var enteredText = controller.text;
  var enteredNumber = int.parse(enteredText)

  for (var i = 1; i <= 10; i++) {
    print('$enteredNumber*$i = ${enteredNumber * i}');
  }
}

相关问题