flutter - language_tool 如何导入 class
flutter - language_tool how to import a class
我对 flutter 没有多少经验。
我想为 Dart 和 Flutter 使用 language_tool 库。(https://pub.dev/packages/language_tool)
我创建了下面的脚本,并希望在屏幕上显示一个包含所有 .issueType 的 ListTile。
但我不知道如何从 language_tool 包中导入 WritingMistake class。
你知道我怎么解决吗?
void main() => runApp(mainApp());
class mainApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Chat(),
);
}
}
class Chat extends StatefulWidget {
const Chat({Key? key}) : super(key: key);
@override
_ChatState createState() => _ChatState();
}
class _ChatState extends State<Chat> {
String text = 'Helllo I am Gabriele';
Future<List<WritingMistake>> tool(String text) async {
var tool = LanguageTool();
var result = tool.check(text);
var correction = await result;
print(correction);
List<WritingMistake> mistakes = [];
for (var m in correction) {
WritingMistake mistake = WritingMistake(m['offset'], m['length'],
m['issueType'], m['issueDescription'], m['replacements']);
mistakes.add(mistake);
}
print(mistakes.length);
return mistakes;
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: FutureBuilder(
future: tool(text),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.data == null) {
return Container(
child: Center(
child: Text('Loading...'),
),
);
} else {
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, int index) {
return ListTile(
title: Text(snapshot.data[index].issueType),
);
},
);
}
},
),
),
);
}
}
希望有人能帮助我。谢谢。
我下载了这个包,复制了你的代码,我只有一个错误,在这一行:
WritingMistake mistake = WritingMistake(m['offset'], m['length'],
m['issueType'], m['issueDescription'], m['replacements']);
我用这段代码解决了这个问题:
WritingMistake mistake = WritingMistake(message: m.message, offset: m.offset, length: m.length,
issueType: m.issueType, issueDescription: m.issueDescription, replacements: m.replacements);
程序输出:
如果我将短语:"Helllo I am Gabriele"
更改为 "Hello I am Gabriele"
,则输出将是“未分类”。
希望对您有所帮助!
我对 flutter 没有多少经验。
我想为 Dart 和 Flutter 使用 language_tool 库。(https://pub.dev/packages/language_tool)
我创建了下面的脚本,并希望在屏幕上显示一个包含所有 .issueType 的 ListTile。
但我不知道如何从 language_tool 包中导入 WritingMistake class。
你知道我怎么解决吗?
void main() => runApp(mainApp());
class mainApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Chat(),
);
}
}
class Chat extends StatefulWidget {
const Chat({Key? key}) : super(key: key);
@override
_ChatState createState() => _ChatState();
}
class _ChatState extends State<Chat> {
String text = 'Helllo I am Gabriele';
Future<List<WritingMistake>> tool(String text) async {
var tool = LanguageTool();
var result = tool.check(text);
var correction = await result;
print(correction);
List<WritingMistake> mistakes = [];
for (var m in correction) {
WritingMistake mistake = WritingMistake(m['offset'], m['length'],
m['issueType'], m['issueDescription'], m['replacements']);
mistakes.add(mistake);
}
print(mistakes.length);
return mistakes;
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: FutureBuilder(
future: tool(text),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.data == null) {
return Container(
child: Center(
child: Text('Loading...'),
),
);
} else {
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, int index) {
return ListTile(
title: Text(snapshot.data[index].issueType),
);
},
);
}
},
),
),
);
}
}
希望有人能帮助我。谢谢。
我下载了这个包,复制了你的代码,我只有一个错误,在这一行:
WritingMistake mistake = WritingMistake(m['offset'], m['length'],
m['issueType'], m['issueDescription'], m['replacements']);
我用这段代码解决了这个问题:
WritingMistake mistake = WritingMistake(message: m.message, offset: m.offset, length: m.length,
issueType: m.issueType, issueDescription: m.issueDescription, replacements: m.replacements);
程序输出:
如果我将短语:"Helllo I am Gabriele"
更改为 "Hello I am Gabriele"
,则输出将是“未分类”。
希望对您有所帮助!