All tutorials

Flutter

intermediate

The Flutter API wrapper is useful for Dart or Flutter developers who wish to seamlessly integrate the what3words API into their Dart or Flutter applications, without the hassle of having to manage the low level API calls themselves.

A full example of a Flutter application is available in our GitHub repository.

The what3words API is a fast, simple interface which allows you to convert 3 word addresses such as ///index.home.raft to latitude and longitude coordinates such as (-0.203586, 51.521251) and vice versa. It features a powerful AutoSuggest function, which can validate and correct user input and limit it to certain geographic areas (this powers the search box on our map site). It allows you to request a section of the what3words grid (which can be requested as GeoJSON for easy display on online maps), and to request the list of all languages supported by what3words. For advanced users, AutoSuggest can be used to post-process voice output. See links on the left to navigate.

All coordinates are latitude,longitude pairs in standard WGS-84 (as commonly used worldwide in GPS systems). All latitudes must be in the range of -90 to 90 (inclusive).

1

You will need a what3words API key to complete this tutorial.

2

Installation

The artifact is available through pub.dev. Update your pubspec.yaml file with

dependencies:
 what3words: 3.1.0
Copied
3

Setup

Import the what3words library

import 'package:what3words/what3words.dart';
Copied

Instantiate an instance of What3WordsV3, from which all API requests can be made

var api = What3WordsV3('what3words-api-key');
Copied
4

Usage

Convert to 3 word address

This function converts coordinates (expressed as latitude and longitude) to a 3 word address.

More information about convertTo3wa, including returned results is available in the what3words REST API documentation.

Find the words for (51.508344,0.12549900):

var words = await api
      .convertTo3wa(Coordinates(51.508344, -0.12549900))
      .language('en')
      .execute();
print('Words: ${words.data()?.toJson()}');
Copied

Convert to coordinates

This function converts a 3 word address to a position, expressed as coordinates of latitude and longitude.

This function takes the words parameter as a string of 3 words 'table.book.chair'

More information about convertToCoordinates, including returned results is available in the what3words REST API documentation.

Find the words for ///filled.count.soap:

var coordinates = await api.convertToCoordinates('table.book.chair').execute();
print('Coordinates ${coordinates.data()?.toJson()}');
Copied

AutoSuggest

When presented with a 3 word address that may be incorrectly entered, AutoSuggest returns a list of potential correct 3 word addresses. It needs the first two words plus at least the first character of the third word to produce suggestions.

This method provides corrections for mis-typed words (including plural vs singular), and words being in the wrong order.

Optionally, clipping can narrow down the possibilities, and limit results to:

  • One or more countries
  • A geographic area (a circle, box or polygon)

This dramatically improves results, so we recommend that you use clipping if possible.

To improve results even further, set the focus to user’s current location. This will make autosuggest return results which are closer to the user.

More information about autosuggest, including returned results is available in the what3words REST API documentation.

Example: Basic Autosuggest call

var autosuggest =
      await api.autosuggest('fun.with.code').nResults(3).execute();
print('Autosuggest: ${autosuggest.data()?.toJson()}');
Copied

Example: AutoSuggest, clipping the results returned to France and Germany

var autosuggest = await api
      .autosuggest('fun.with.code')
      .clipToCountry(['fr', 'de']).execute();
print('Autosuggest: ${autosuggest.data()?.toJson()}');
Copied

Example: AutoSuggest, Focus on (51.4243877,-0.34745)

var autosuggest = await api
      .autosuggest('fun.with.code')
      .focus(Coordinates(51.4243877, -0.34745))
      .execute();
print('Autosuggest: ${autosuggest.data()?.toJson()}');
Copied

Grid section

Grid Section returns a section of the what3words 3m x 3m grid as a set of horizontal and vertical lines covering the requested area, which can then be drawn onto a map.

The requested box must not exceed 4km from corner to corner, or a BadBoundingBoxTooBig error will be returned.

More information about gridSection, including returned results is available in the what3words REST API documentation.

Get a grid for (51.527649, -0.191746) in the south-west, and (51.515900, -0.212517 in the north-east:

// Obtain a grid section within the provided bounding box
var gridSection = await api
      .gridSection(Coordinates(51.515900, -0.212517), Coordinates(51.527649, -0.191746))
      .execute();
print(gridSection.data()?.toJson());
Copied

Available languages

This function returns the currently supported languages. It will return the two letter code, and the name of the language both in that language and in English.

More information about availableLanguages, including returned results is available in the what3words REST API documentation.

var languages = await api.availableLanguages().execute();
print('Languages: ${languages.data()?.toJson()}');
Copied

Handling errors

Success of failure of an API call can be determined through the use of the isSuccessful() function.

If it’s been determined that an API call was not successful, code and message values which represent the error, are accessible from through the getError() function.

var autosuggest = await api
  .autosuggest('freshen.overlook.clo')
  .clipToCountry(['fr', 'de']).execute();

if (autosuggest.isSuccessful()) {
  ...
} else {
  var error = autosuggest.getError();

  if (error == What3WordsError.BAD_CLIP_TO_COUNTRY) {
    // Invalid country clip is provided
    print('BadClipToCountry: ${error.message}');
  }
}
Copied

Related tutorials

Back to top