Extracting Data from QR Code Query Parameters in Dart/Flutter

Rui Silva
3 min readNov 19, 2023

QR codes have become ubiquitous in our daily lives, providing a convenient way to encode and share information. They are used on everything from product packaging to restaurant menus, and they can even be used to store data for mobile applications.

In this article, we will explore how to use Dart/Flutter to extract data from a query parameter in a QR code. This can be useful for a variety of applications, such as opening a specific URL or retrieving additional information about a product.

Scanning the QR Code

The first step is to scan the QR code using a QR code scanner plugin for Flutter. There are a number of different scanner plugins available, but one popular option is the qr_code_scanner plugin.

To use the qr_code_scanner plugin, you will need to add it to your project's pubspec.yaml file:

YAML

dependencies:
qr_code_scanner: ^8.0.1+5

Once you have added the plugin, you can use the QRCodeScanner widget to scan a QR code:

Dart

import 'package:qr_code_scanner/qr_code_scanner.dart';
void main() {
// Scan the QR code
final result = await QRCodeScanner.scan();
print(result);
}

This code will scan a QR code and print the result to the console.

Parsing the URL

Once you have the QR code result, you can parse it to extract the URL. The QR code result is typically a string representation of the URL.

To parse the URL, you can use the Uri class:

Dart

final url = Uri.parse(result);

This code will create a Uri object from the URL string.

Extracting the Data Structure

The query parameters of a URL are typically encoded as key-value pairs. To extract the data structure from the query parameters, you can use the queryParameters property of the Uri object:

Dart

final queryParameters = url.queryParameters;

This code will create a Map object containing the query parameters. The keys of the map are the parameter names, and the values are the parameter values.

Using the Data Structure

Once you have extracted the data structure, you can use it however you need. For example, if the data structure is a JSON object, you can decode it using the jsonDecode function:

Dart

final dataString = queryParameters['data'];
final jsonData = jsonDecode(dataString);
print(jsonData);

This code will decode the JSON object and print it to the console.

Conclusion

Extracting data from a query parameter in a QR code is a relatively simple process using Dart/Flutter. By following the steps outlined in this article, you can easily extract the data and use it in your application.

This technique can be useful for a variety of applications, such as:

  • Opening a specific URL: You can extract the URL from the query parameter and open it in a web browser.
  • Retrieving additional information about a product: You can extract the product ID from the query parameter and fetch additional information about the product from a database.
  • Accessing additional features in an app: You can extract a token from the query parameter and use it to unlock additional features in your app.

The possibilities are endless!

--

--