I am reading Chapter 11 of the Flutter Cookbook – Second Edition by Simone Alessandria. I follow the following steps:
-
Create a new Flutter app, and call it map_recipe.
-
flutter pub add google_maps_flutter
-
I obtained Google Maps API key from here.
-
Open the
android/app/src/main/AndroidManifest.xml
file in your project.
Add the following line under the icon launcher icon, in the application node:android:icon=”@mipmap/ic_launcher”>
-
Set the minSdkVersion in the
android/app/build.gradle
file:android {
defaultConfig {
minSdkVersion 20
}
}
But how to do it for Web?
Here is the code:
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:http/http.dart';
import 'package:http/http.dart' as http;
// 1. flutter pub add http
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Future Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: const FuturePage(),
);
}
}
class FuturePage extends StatefulWidget {
const FuturePage({super.key});
@override
State<FuturePage> createState() => _FuturePageState();
}
class _FuturePageState extends State<FuturePage> {
String result = '';
late Completer completer;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Back from the Future'),
),
body: Center(
child: Column(children: [
const Spacer(),
ElevatedButton(
child: const Text('GO!'),
onPressed: () {
returnError().then((value) {
setState(() {
result = 'Success';
});
}).catchError((onError) {
setState(() {
result = onError.toString();
});
}).whenComplete(() => print('Complete'));
},
),
const Spacer(),
Text(result),
const Spacer(),
const CircularProgressIndicator(),
const Spacer(),
]),
),
);
}
Future<Response> getData() async {
const authority = 'www.googleapis.com';
const path = '/books/v1/volumes/junbDwAAQBAJ';
Uri url = Uri.https(authority, path);
return http.get(url);
}
Future<Error> returnError() async {
await Future.delayed(const Duration(seconds: 2));
throw Exception('Something terrible happened!');
}
}
The full code can be found on the author’s GitHub