How to integrate wit.ai with Flutter

wit.ai

wit.ai is an NLP engine by Facebook that allows users to convert statements into a queried structural data that enables developers to build conversational applications (like chatbots).

Logo

To get started, you can visit wit.ai and log in with your existing Facebook account. You can also make use of this amazing documentation provided by the wit.ai team.

Integration

  • To add wit.ai to your Flutter app, install the flutter_witai package from pub.dev.

  • Now. create a wit object and set it to the WitManager class from the package that consists of two parameters, namely utterance and headers.

final wit = WitManager(utterance: "hello",headers: "XXXXXXXXXX");

Utterance is the query statement for which you’ll are creating a structured data response and headers are be the wit.ai project SERVER ACCESS TOKEN that can be found in the settings.

  • Call the fetchLink() function to trigger the wit.ai function and get the structured JSON response based on whatever utterance query you mentioned.
wit.fetchLink();
  • You can view the results by printing out the json to the console and setting the fetchLink() function to a dynamic variable.
dynamic response = await wit.fetchLink();
print(response);

The response should look somewhat like this:

{"text":"hello","intents":[{"id":"985952918880417","name":"search","confidence":1}],"entities":{},"traits":{"wit$sentiment":[{"id":"5ac2b50a-44e4-466e-9d49-bad6bd40092c","value":"positive","confidence":0.5435}]}}

  • You can now get any values from the response json, such as entities, traits, etc.
print(response['entities']);

It’s that simple. You can now create any Flutter button (such as TextButton) and then simply call the function within its onPressed parameter to call the wit.ai function.

Remember to use the async keyword for asynchronous operations.

You can also take a look at the example code written in the package.

Free Resources

Attributions:
  1. undefined by undefined