A Flutter plugin for getting the headers required to call Google APIs with an app restricted API key.
Add this to your project's pubspec.yaml
file:
dependencies:
google_api_headers: ^3.7.0
Depending on the platform (iOS or Android), the function will return the required key and value pairs for calling Google APIs with keys that are restricted to an iOS or Android app.
import 'package:google_api_headers/google_api_headers.dart';
final headers = await GoogleApiHeaders().getHeaders();
Run this command:
With Flutter:
$ flutter pub add google_api_headers
This will add a line like this to your package's pubspec.yaml (and run an implicit flutter pub get
):
dependencies:
google_api_headers: ^3.7.0
Alternatively, your editor might support flutter pub get
. Check the docs for your editor to learn more.
Now in your Dart code, you can use:
import 'package:google_api_headers/google_api_headers.dart';
Example/lib/main.dart
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:google_api_headers/google_api_headers.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
MyAppState createState() => MyAppState();
}
class MyAppState extends State<MyApp> {
Map<String, String>? _headers;
@override
void initState() {
super.initState();
getHeaders();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Text(
'Google API headers: ${_headers.toString()}',
textAlign: TextAlign.center,
),
),
),
);
}
Future<void> getHeaders() async {
final headers = await const GoogleApiHeaders().getHeaders();
setState(() => _headers = headers);
}
}
View on GitHub: https://github.com/zeshuaro/google_api_headers