Flutter 如何从用户输入中计算文本字符串中的字数

Flutter 如何从用户输入中计算文本字符串中的字数,flutter,dart,Flutter,Dart,我开始学习飞镖和颤振,但有一个应用程序有问题。我正在尝试编写一个应用程序,计算用户输入的文本字符串中的字数。我为此编写了countWords函数,但我不知道如何正确地向该函数发送文本字符串。 有人能告诉我怎么做并更正我的代码吗 import 'package:flutter/material.dart'; import 'dart:convert'; class MyForm extends StatefulWidget { @override State<StatefulWidge

我开始学习飞镖和颤振,但有一个应用程序有问题。我正在尝试编写一个应用程序,计算用户输入的文本字符串中的字数。我为此编写了countWords函数,但我不知道如何正确地向该函数发送文本字符串。 有人能告诉我怎么做并更正我的代码吗

import 'package:flutter/material.dart';
import 'dart:convert';

class MyForm extends StatefulWidget {
  @override
State<StatefulWidget> createState() => MyFormState();
}

class MyFormState extends State {
 final _formKey = GlobalKey<FormState>();

 final myController = TextEditingController();
 int words_num = 0;
 void countWords() {
   var regExp = new RegExp(r"\w+(\'\w+)?");
   int wordscount = regExp.allMatches(myController.text); //here I have trouble
   setState(() {
     words_num = wordscount;
   });
 }

Widget build(BuildContext context) {
 return Container(
    padding: EdgeInsets.all(10.0),
    child: new Form(
        key: _formKey,
        child: new Column(
          children: <Widget>[
            new Text(
              'Text string:',
              style: TextStyle(fontSize: 20.0),
            ),
            new TextFormField(
              decoration:
                  InputDecoration(labelText: 'Enter your text string'),
              controller: myController,
            ),
            new SizedBox(height: 20.0),
            new RaisedButton(
              onPressed: () {
                countWords();
              },
              child: Text('Count words'),
              color: Colors.blue,
              textColor: Colors.white,
            ),
            new SizedBox(height: 20.0),
            new Text(
              'Number of words: $words_num',
              style: TextStyle(fontSize: 20.0),
            ),
          ],
        )));
 }
}

void main() => runApp(new MaterialApp(
  debugShowCheckedModeBanner: false,
  home: new Scaffold(
    appBar: new AppBar(title: new Text('Count words app')),
    body: new MyForm())));
导入“包装:颤振/材料.省道”;
导入“dart:convert”;
类MyForm扩展了StatefulWidget{
@凌驾
State createState()=>MyFormState();
}
类MyFormState扩展了状态{
final _formKey=GlobalKey();
最终myController=TextEditingController();
int words_num=0;
void countWords(){
var regExp=new regExp(r“\w+(\'\w+));
int-wordscont=regExp.allMatches(myController.text);//这里有问题
设置状态(){
单词数量=单词数量;
});
}
小部件构建(构建上下文){
返回容器(
填充:所有边缘设置(10.0),
儿童:新表格(
键:_formKey,
子:新列(
儿童:[
新文本(
'文本字符串:',
样式:TextStyle(fontSize:20.0),
),
新TextFormField(
装饰:
InputDecoration(labelText:“输入文本字符串”),
控制器:myController,
),
新尺寸盒子(高度:20.0),
新升起的按钮(
已按下:(){
countWords();
},
子项:文本('Count words'),
颜色:颜色,蓝色,
textColor:Colors.white,
),
新尺寸盒子(高度:20.0),
新文本(
'字数:$words_num',
样式:TextStyle(fontSize:20.0),
),
],
)));
}
}
void main()=>runApp(新材料应用(
debugShowCheckedModeBanner:false,
家:新脚手架(
appBar:new appBar(标题:新文本('Count words app')),
正文:新MyForm());

现在您正在将
Iterable
分配给
int
。由于需要长度,请使用的
length
属性

这是假设你的正则表达式是有效的,在我看来是有效的。如果不是,您可以尝试以下方法:

[\w-]+
[\w-]+