[Huawei Developers] ask011:
Introduction

In this article, we will be integration Text Search i.e. Keyword search feature of Site Kit. Huawei Site Kit provides core capabilities to developer to quickly build apps with which users can explore world around them seamlessly. Huawei Site kit provides following capabilities to developer as shown below.

Keyword search: returns the place list based on the keywords entered by user.

Nearby place search: Searches for nearby location based on current location of the user’s device.

Place detail search: Search for details about the place.

Place search suggestion: Returns list of suggested places.

Autocomplete: Returns an autocomplete place and a list of suggested places based on the entered keyword.

Development Overview

You need to install Flutter and Dart plugin in IDE and I assume that you have prior knowledge about the Flutter and Dart.

Hardware Requirements

A computer (desktop or laptop) running Windows 10.

A Huawei phone (with the USB cable), which is used for debugging.

Software Requirements

Java JDK 1.7 or later.

Android studio software or Visual Studio or Code installed.

HMS Core (APK) 4.X or later.

Integration process

Step 1. Create flutter project

View attachment 5315431

View attachment 5315433

Step 2. Add the App level gradle dependencies.

Choose inside project Android > app > build.gradle.

apply plugin: 'com.android.application'
apply plugin: 'com.huawei.agconnect'

Add root level gradle dependencies

maven {url 'https://developer.huawei.com/repo/'}
classpath 'com.huawei.agconnect:agcp:1.4.1.300'

Step 3: Add the below permissions in Android Manifest file.

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_COARES_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

Step 4: Add Site Kit Plugin path in pubspec.yaml file under dependencies.

Step 5: Create a project in AppGallery Connect.

https://developer.huawei.com/consumer/en/codelab/HMSPreparation/index.html#0

pubspec.yaml

name: sample_one
description: A new Flutter application.

# The following line prevents the package from being accidentally published to
# pub.dev using `pub publish`. This is preferred for private packages.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev

version: 1.0.0+1

environment:
sdk: ">=2.7.0 <3.0.0"

dependencies:
flutter:
sdk: flutter
huawei_map:
path: ../huawei_map/
huawei_location:
path: ../huawei_location/
huawei_safetydetect:
path: ../huawei_safetydetect
huawei_site:
path: ../huawei_site
http: ^0.12.2
rflutter_alert: ^2.0.2
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2
# add this line to your dependencies
toast: ^0.1.5

dev_dependencies:
flutter_test:
sdk: flutter

# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec

# The following section is specific to Flutter.
flutter:

Declare and instantiate service object

Future<void> initSearchService() async {
searchService = await SearchService.create(Uri.encodeComponent(API_KEY));
}

Create TextSearchRequest and its body

Future<void> valueChanged(String value) async {
try {
TextSearchRequest request = new TextSearchRequest();
request.query = value;
request.location = Coordinate(lat: 12.9098, lng: 77.7440);
request.language = "en";
request.countryCode = "IN";
request.pageIndex = 1;
request.pageSize = 2;
request.radius = 5000;
TextSearchResponse response11 = await searchService.textSearch(request);
Map<String, dynamic> data = json.decode(response11.toJson());
print('-----------------------------------------------');
int i = 0;
for (String key in data.keys) {
if (key == 'sites') {
setState(() {
searchResponse = data['sites'][i]['name'].toString() +
'\n' +
data['sites'][i]['formatAddress'].toString() +
'\n' +
data['sites'][i]['address']['tertiaryAdminArea'].toString() +
'\n' +
data['sites'][i]['location']['lat'].toString() +
'\n' +
data['sites'][i]['location']['lng'].toString();
});
location = new LatLng(data['sites'][i]['location']['lat'],
data['sites'][i]['location']['lng']);
print(data[key]);
}
}
print('-----------------------------------------------');
} on PlatformException catch (e) {
setState(() {
searchResponse = e.toString();
});
}
}

Previous article on delivery app in flutter

Intermediate: Integration of Huawei map kit and Location kit in DeliveryApp in Flutter (Cross platform)​

Result

View attachment 5315437

View attachment 5315439

Tricks and Tips

Make sure you have downloaded latest plugin.

Make sure that updated plugin path in yaml.

Make sure that plugin unzipped in parent directory of project.

Makes sure that agconnect-services.json file added.

Make sure dependencies are added build file.

Run flutter pug get after adding dependencies.

Generating SHA-256 certificate fingerprint in android studio and configure in Ag-connect.

Conclusion

We have learnt how to integrate Huawei Site kit Text Search feature in delivery app in flutter. Where user can search for specific hotel in the search box and clicks on the result to see the list of orders. Similar way you can use Huawei Site kit as per user requirement in your application. In this part-1 I have covered Text Search that is Keyword Search you can expect more features implementation in part-2.

Thank you so much for reading, I hope this article helps you to understand the Huawei Site kit features in flutter.

Reference

Site Kit

Site Kit Plugin