[go: up one dir, main page]

0% found this document useful (0 votes)
22 views21 pages

CSE-4078 Mobile Application Development: Lectures - Week 10

The document discusses mobile application development lectures for week 10. It covers topics like using the geolocator package to get the current and last known location of a user, adding location permissions to the Android manifest, checking and requesting location permissions, making API calls to get weather data, parsing JSON responses, and maintaining internal state in stateful widgets. It provides code examples for getting location, making HTTP requests using the http package, and parsing JSON responses.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views21 pages

CSE-4078 Mobile Application Development: Lectures - Week 10

The document discusses mobile application development lectures for week 10. It covers topics like using the geolocator package to get the current and last known location of a user, adding location permissions to the Android manifest, checking and requesting location permissions, making API calls to get weather data, parsing JSON responses, and maintaining internal state in stateful widgets. It provides code examples for getting location, making HTTP requests using the http package, and parsing JSON responses.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

CSE-4078

MOBILE APPLICATION
DEVELOPMENT
LECTURES – WEEK 10

SARA MASOOD
APP OF THE MODULE
GEO LOCATION

 Use the package GeoLocator


 geolocator | Flutter Package (pub.dev)

dependencies:
geolocator: ^8.0.0
import 'package:geolocator/geolocator.dart';
GEO LOCATION

 Current location
Position position = await Geolocator.getCurrentPosition(desiredAccuracy:
LocationAccuracy.high);

 Last known location


Position position = await Geolocator.getLastKnownPosition();
ADDING PERMISSIONS TO ANDROID MANIFEST
 Go to android > app > src > main > AndroidMenifest.xml and add following permissions (like added in below-given
picture)
 Location Permission
 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
 Internet permission
 <uses-permission android:name="android.permission.INTERNET"/>
PERMISSION FOR LOCATION SERVICES

 Checking the permission


 LocationPermission permission = await Geolocator.checkPermission();

 Request the permission


 LocationPermission permission = await Geolocator.requestPermission();
GEO LOCATION MUST BE ASYNC

void getLocation() async{


Position position =
await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.high);
}
ASYNCHRONOUS PROGRAMMING: FUTURES, ASYNC, AWAIT

 Asynchronous operations let your program complete work while waiting for another operation to
finish.
void example() async {
await …
}
 Future
 A future (lower case “f”) is an instance of the Future (capitalized “F”) class.
 A future represents the result of an asynchronous operation, and can have two states: uncompleted or
completed.
ASYNCHRONOUS PROGRAMMING: FUTURES, ASYNC, AWAIT

Future<void> printOrderMessage() async {


print('Awaiting user order...');
var order = await fetchUserOrder();
print('Your order is: $order');
}
INTERNAL STATE FOR A STATEFULWIDGET

 State is information that


 can be read synchronously when the widget is built
 might change during the lifetime of the widget.

 State objects have the different lifecycle


 State class - widgets library - Dart API (flutter.dev)
INTERNAL STATE FOR A STATEFULWIDGET

 initState() → void
 Called when this object is inserted into the tree

 activate() → void
 Called when this object is reinserted into the tree after having been removed via deactivate

 build(BuildContext context) → Widget


 Describes the part of the user interface represented by this widget

 deactivate() → void
 Called when this object is removed from the tree

 dispose() → void
 Called when this object is removed from the tree permanently
APIS
API (APPLICATION
PROGRAM
INTERFACE)

 An API is a set of routines,


protocols, and tools for
building software
applications.
 You must have API Keys.
API
GET WEATHER API KEY

 Go to Weather API – OpenWeatherMap and create


get API key.
USING HTTP PACKAGE

 A composable, Future-based library for making HTTP requests.


 This package contains a set of high-level functions and classes that make it easy to consume
HTTP resources.
 Installing
dependencies:
http: ^0.13.4

import 'package:http/http.dart’ as http;


 http | Dart Package (pub.dev)
 Fetch data from the internet | Flutter
USING HTTP PACKAGE - MAKE A NETWORK REQUEST

Future<http.Response> fetchWeather() async {


return await http.get(Uri.parse(' api.openweathermap.org/data/2.5/weather?
q={city name}&appid={API_key}'));
}

 The http.Response class contains the data received from a successful http call.
GETTING THE RESPONSE CODE AND BODY

 Get the response body


 response.body

 Get the resoonse code


 response.statusCode
HTTP RESPONSE CODES

 Informational responses (100–199)


 Successful responses (200–299)
 Redirection messages (300–399)
 Client error responses (400–499)
 Server error responses (500–599)
JSON (JAVASCRIPT OBJECT
NOTATION)

 '{"name":"John", "age":30, "car":null}'


 It defines an object with 3 properties:
 name
 age
 car
PARSING THE JSON

 Use the dart:convert library


 dart:convert library - Dart API

 import 'dart:convert’;
 Use the jsonDecode function
 Parses the string and returns the resulting Json object.
 var longitude = jsonDecode(data)[‘coord’][‘lon’];
 jsonDecode function - dart:convert library - Dart API

You might also like