flutterclassdartlanguagetool

flutter - language_tool how to import a class


I don't have much experience with flutter.

I would like to use the language_tool library for Dart and Flutter .(https://pub.dev/packages/language_tool)

I created the script below and would like a ListTile with all the .issueTypes to appear on the screen.

But I don't know how to import the WritingMistake class from the language_tool package.

Do you know how I can solve?

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

Hope someone can help me. Thank you.


Solution

  • I downloaded the package, copied your code, I got only one error, in the line:

    WritingMistake mistake = WritingMistake(m['offset'], m['length'],
              m['issueType'], m['issueDescription'], m['replacements']);
    

    And I solved it with this code:

      WritingMistake mistake = WritingMistake(message: m.message, offset: m.offset, length: m.length,
          issueType: m.issueType, issueDescription: m.issueDescription, replacements: m.replacements);
    

    Program Output:

    enter image description here

    If I change the phrase: "Helllo I am Gabriele" to "Hello I am Gabriele", then the output will be "uncategorized".

    Hope this helped!