Return Text with current position Flutter - flutter

I want to return some Strings or Texts with :
name of the city
postal code
country
So this is my code :
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:geocoder/geocoder.dart';
import 'package:geolocator/geolocator.dart';
class Maps extends StatefulWidget {
static Future<void> show(
BuildContext context,
) async {
await Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => Maps(),
fullscreenDialog: true,
),
);
}
#override
_MapsState createState() => _MapsState();
}
Future<Position> locateUser() async {
return Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
}
class _MapsState extends State<Maps> {
Geolocator geolocator = Geolocator();
Position _currentPosition;
String _location;
String _addressLine;
bool done = false;
#override
Widget build(BuildContext context) {
_getCurrentLocation();
_getLocation(_currentPosition);
return Scaffold(
body: Center(
child: Container(
height: MediaQuery.of(context).size.height * 0.4,
width: MediaQuery.of(context).size.width * 0.6,
child: Center(
child: Column(
children: [
done == false
? Text("Need to get the position")
: Text("val 1: $_location, val 2: $_addressLine"),
FlatButton(
child: Text("Get location"),
onPressed: () {
_getCurrentLocation();
},
),
],
),
),
),
),
);
}
Future<void> _getLocation(Position position) async {
debugPrint('location: ${position.latitude}');
final coordinates = new Coordinates(position.latitude, position.longitude);
List<Address> addresses =
await Geocoder.local.findAddressesFromCoordinates(coordinates);
Address first = addresses.first;
_location = "${first.featureName}";
_addressLine = " ${first.addressLine}";
done = true;
}
void _getCurrentLocation() {
Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.best)
.then((Position position) {
setState(() {
_currentPosition = position;
});
}).catchError((e) {
print(e);
});
}
}
The problem is that it's always returned "Need to get the position" instead of real address.
I know this code isn't the best but it's what comes from hours of researches, I didn't find some clear explanations about the use of geo functions.

I only just made few changes to your code, as i dont want to ruin your code
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:geocoder/geocoder.dart';
import 'package:geolocator/geolocator.dart';
class Maps extends StatefulWidget {
static Future<void> show(
BuildContext context,
) async {
await Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => Maps(),
fullscreenDialog: true,
),
);
}
#override
_MapsState createState() => _MapsState();
}
Future<Position> locateUser() async {
return Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
}
class _MapsState extends State<Maps> {
Geolocator geolocator = Geolocator();
Position _currentPosition;
String _location;
String _addressLine;
bool done = false;
#override
void initState() {
_getCurrentLocation();
super.initState();
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Container(
height: MediaQuery.of(context).size.height * 0.4,
width: MediaQuery.of(context).size.width * 0.6,
child: Center(
child: Column(
children: [
done == false
? Text("Need to get the position")
: Text("val 1: $_location, val 2: $_addressLine"),
FlatButton(
child: Text("Get location"),
onPressed: () {
_getCurrentLocation();
},
),
],
),
),
),
),
);
}
Future<void> _getLocation(Position position) async {
debugPrint('location: ${position.latitude}');
final coordinates = new Coordinates(position.latitude, position.longitude);
List<Address> addresses =
await Geocoder.local.findAddressesFromCoordinates(coordinates);
Address first = addresses.first;
_location = "${first.featureName}";
_addressLine = " ${first.addressLine}";
setState(() {
done = true;
});
}
void _getCurrentLocation() {
Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.best)
.then((Position position) {
_getLocation(position);
}).catchError((e) {
print(e);
});
}
}

Related

loadingbar not working using getx flutter

hello I am working on a project using google maps inside it I implemented a loading bar using getx but it's not working I don't know why
here is my map page :
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:helping_hand/StateManagement/BarController.dart';
import 'package:helping_hand/UI/Other/Emergency.dart';
import 'package:list_picker/list_picker.dart';
import '../../StateManagement/MapController.dart';
import '../../drawers/bottomnavbar.dart';
import '../Bottom Navigation/My status.dart';
class Mappage extends StatelessWidget {
Mappage({Key? key}) : super(key: key);
final TextEditingController selectPage = TextEditingController();
final List<String> pageList = ['helps','sitrep','location','emergencies', 'users',];
#override
Widget build(BuildContext context) {
MapController mapcontroller = Get.find();
BarController barController = Get.find();
barController.checkUserData();
return Scaffold(
body: Stack(
children: [
SizedBox(height: MediaQuery.of(context).size.height,
child: SingleChildScrollView(
child: SizedBox(height: MediaQuery.of(context).size.height,width: MediaQuery.of(context).size.width,
child: Column(
children: [
Stack(
children: [
SizedBox(
height: MediaQuery.of(context).size.height,
child: GetBuilder<MapController>(builder: (_)=>GoogleMap(initialCameraPosition:MapController.initial,
mapType: MapType.normal,markers:mapcontroller.markers,
onMapCreated: (GoogleMapController controller){
mapcontroller.completercontrol.complete(controller);
mapcontroller.googleMapController = controller;
},),)
),
Positioned(
top: 50,
height: 60,
width: MediaQuery.of(context).size.width,
child: Row(mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
SizedBox(width: MediaQuery.of(context).size.width/2,
child: ListPickerField(label:'select', items:pageList,controller:selectPage,)),
ElevatedButton(
style:const ButtonStyle(backgroundColor:MaterialStatePropertyAll(Colors.redAccent)) ,
onPressed: () {
if(selectPage.text.isNotEmpty){
mapcontroller.fetchMap(selectPage.text);
}else{
Get.snackbar('error','select a page from dropdown menu');
}
},
child:const Text('Get',style: TextStyle(color: Colors.white),)),
FloatingActionButton(
heroTag: 'btn1',
onPressed:(){
Get.to(()=>Emergency());
},
child: const Icon(Icons.emergency_outlined),
)
],
),
),
Positioned(
bottom: 50,
width: MediaQuery.of(context).size.width,
child: Row(mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
ElevatedButton(
style:const ButtonStyle(backgroundColor:MaterialStatePropertyAll(Colors.redAccent)) ,
onPressed: () {
mapcontroller.googleMapController.dispose();
Get.offAll(()=>const Nav());
},
child:const Text('Go to Dashboard',style: TextStyle(color: Colors.white),)),
FloatingActionButton(
heroTag: 'btn2',
onPressed: () {
mapcontroller.getlatlong();
},
child:const Icon(Icons.location_on,color: Colors.white,)),
],
),
),
],
),
],
),
),
),
),
GetBuilder<MapController>(builder: (_){
if (mapcontroller.isloading == true) {
return Container(
color: Colors.white.withOpacity(0.5),
child: const Center(
child: CircularProgressIndicator(backgroundColor: Colors.redAccent,color: Colors.white,),
),
);
} else {
return const SizedBox.shrink();
}
}),
],
));
}
}
class DocsForMap {
final double lat;
final double long;
final String? desc;
final String title;
final int? uploaddate;
final int? deletetime;
final List<dynamic>? neededSupply;
final List<dynamic>? downloadurl;
DocsForMap(this.desc, this.uploaddate, this.deletetime, this.neededSupply, this.downloadurl,{
required this.lat,
required this.long,
required this.title,
});
}
here is the controller (look at the bottom for function FetchAlerts and loading bar) :
import 'dart:async';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
import 'package:get/get.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:helping_hand/UI/Map/Mappage.dart';
class MapController extends GetxController{
String locationmessage = 'currentlocation of user';
late String lat ;
late String long ;
Set<Marker>markers= <Marker>{};
late BitmapDescriptor customIcon;
#override
void onInit() {
// BitmapDescriptor.fromAssetImage(const ImageConfiguration(size: Size(3.5, 3.5)),
// 'assets/undraw/warning.png')
// .then((d) {
// customIcon = d;
// update();
// });
lat = '10.228370';
long ='76.198799';
super.onInit();
}
// make sure to initialize before map loading
late GoogleMapController googleMapController;
final Completer<GoogleMapController> _controller = Completer();
Completer<GoogleMapController> get completercontrol => _controller;
static CameraPosition initial = const CameraPosition(target:LatLng(10.228370,76.198799),zoom: 15);
//lower part
Future<Position> getCurrentLocation() async {
bool serviceEnabled = await Geolocator.isLocationServiceEnabled();
if (!serviceEnabled) {
return Future.error('the location is not enabled');
}
LocationPermission permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
if (permission == LocationPermission.denied) {
return Future.error(
'location permissions are permanantly denied, cannot grant acess');
}
}
if (permission == LocationPermission.deniedForever) {
return Future.error(
'location permissions are permanantly denied, cannot grant acess');
}
return await Geolocator.getCurrentPosition();
}
void liveLocation() {
loadingbar();
LocationSettings settings = const LocationSettings(
accuracy: LocationAccuracy.high,
distanceFilter: 100,
);
Geolocator.getPositionStream(locationSettings: settings)
.listen((Position position) {
lat = position.latitude.toString();
long = position.longitude.toString();
googleMapController.animateCamera(CameraUpdate.newCameraPosition(CameraPosition(target:LatLng(position.latitude,position.longitude),zoom: 15)));
markers.add(Marker(markerId:const MarkerId('current user location'),position: LatLng(position.latitude,position.longitude)));
update();
});
loadingbaroff();
}
void getlatlong(){
loadingbar();
getCurrentLocation().then((value){
lat = '${value.latitude}';
long = '${value.longitude}';
googleMapController.animateCamera(CameraUpdate.newCameraPosition(CameraPosition(target:LatLng(value.latitude,value.longitude),zoom: 15)));
markers.add(Marker(markerId:const MarkerId('current user location'),position: LatLng(value.latitude,value.longitude)));
update();
if (kDebugMode) {
print(lat);
}
if (kDebugMode) {
print(long);
}
liveLocation();
});
loadingbaroff();
}
void liveLocationToUpload(){
LocationSettings settings = const LocationSettings(
accuracy: LocationAccuracy.high,
distanceFilter: 100,
);
Geolocator.getPositionStream(locationSettings: settings)
.listen((Position position) {
lat = position.latitude.toString();
long = position.longitude.toString();
update();
});
}
//MAP PAGE CONTROLLER CONTROLLS ::::::::::::::
List<DocsForMap> docsformap=[];
Future<void> fetchMap(String page)async {
try{
loadingbar();
docsformap.clear();
int unixTime = DateTime.now().millisecondsSinceEpoch ~/ 1000;
if (kDebugMode) {
print('inside try catch');
}
if (kDebugMode) {
print(page);
}
final FirebaseFirestore db = FirebaseFirestore.instance;
db.collection("map").doc('maps').collection(page).get().then((querySnapshot) {
for (var document in querySnapshot.docs) {
if(document.data()['deleteTime'] > unixTime){
docsformap.add(DocsForMap(document.data()['description'], document.data()['uploadDate'], document.data()['deleteTime'],
document.data()['needed_supply'], document.data()['downloadUrl'], lat: double.parse(document.data()['lat']),
long: double.parse(document.data()['long']), title:document.data()['title']));
}
}
//adding markers
markers.clear();
for(int i = 0;i<docsformap.length;i++){
markers.add(Marker(markerId: MarkerId('$i'),
position: LatLng(docsformap[i].lat,docsformap[i].long),
infoWindow:InfoWindow(title: docsformap[i].title,snippet: docsformap[i].desc),
// icon:customIcon,
onTap: (){markerBar();}
));
}
for (var element in markers) {
if (kDebugMode) {
print('this is a marker element ${element.infoWindow.snippet}');
}
}
update();
});
}catch(e){
Get.snackbar('error','error while fetching $page');
if (kDebugMode) {
print('error happenddddd :::::::: $e');
}
loadingbaroff();
}
loadingbaroff();
}
bool isloading = false;
void loadingbar() {
print('loading bar called');
isloading = true;
update();
}
void loadingbaroff() {
print('loading bar ended');
isloading = false;
update();
}
bool isMarkerSelected = false;
void markerBar() {
isMarkerSelected = true;
update();
}
void markerBaroff() {
isMarkerSelected = false;
update();
}
}
also if you could tell me instead of giving value through initial variable when map creates, can i create the map with current user location using oninit on mapcontroller ?

using Shared preferences with a change notifier

I'm trying to understand how to use shared preferences with a change notifier. I've created a basic app and I want to save a bool and a string from the change notifier using shared preferences.
here is my main.dart:
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
void main() {
runApp(
MultiProvider(
providers: [
ChangeNotifierProvider(create: (_) => GlobalSettings1()),
ChangeNotifierProvider(create: (_) => SectionSettings1()),
],
child: MyApp(),
),
);
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
initialRoute: '/seventh',
routes: {
'/': (context) => Page01(),
'/first': (context) => Page02(),
'/second': (context) => Page03(),
});
}
}
and here is page01
class _Page01State extends State<Page01> {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
InkWell(
onTap: () {
context.read<GlobalSettings1>().ToggleSwitch();
},
child: Container(
height: 30,
width: 80,
color: context.watch<GlobalSettings1>().toggleSwitch01
? Colors.green
: Colors.red,
),
),
SizedBox(
height: 20,
),
InkWell(
onTap: () {
showDialog(
context: context,
builder: (BuildContext context) => Material(
color: Colors.transparent,
child: Buttons(
onTap: (val) {
context.read<GlobalSettings1>().StringToSave(val);
Navigator.pop(context);
},
),
),
);
},
child: Container(
height: 30,
width: 80,
child: Center(
child: Text(
context.watch()<GlobalSettings1>().stringToSave,
),
),
)),
],
),
);
}
}
and finally, here is my change notifier:
class Buttons extends StatelessWidget {
const Buttons({Key? key, required this.onTap}) : super(key: key);
final ValueChanged? onTap;
#override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
InkWell(
onTap: () {
if (onTap != null) {
onTap!('Choice01');
}
},
child: Container(
height: 30,
width: 80,
child: Text('Choice01'),
)),
SizedBox(
height: 30,
),
InkWell(
onTap: () {
if (onTap != null) {
onTap!('Choice02');
}
},
child: Container(
height: 30,
width: 80,
child: Text('Choice02'),
)),
],
);
}
}
class GlobalSettings1 with ChangeNotifier {
bool _toggleSwitch01 = true;
String _stringToSave = 'Choice01';
final SharedPreferences prefs;
GlobalSettings1({required this.prefs});
bool get toggleSwitch01 => _toggleSwitch01;
String get stringToSave => _stringToSave;
void ToggleSwitch() {
_toggleSwitch01 = !_toggleSwitch01;
_setPrefItems();
notifyListeners();
}
void StringToSave(val) {
_stringToSave = val;
_setPrefItems();
notifyListeners();
}
void _setPrefItems() {
prefs.setBool('toggleSwitch01', _toggleSwitch01);
prefs.setString('stringToSave', _stringToSave);
notifyListeners();
}
void _getPrefItems() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
_stringToSave = prefs.getString('stringToSave') ?? '';
_toggleSwitch01 = prefs.getBool('autoUpdateVariables') ?? true;
notifyListeners();
}
bool getToggleSwitch01() {
_getPrefItems();
return _toggleSwitch01;
}
String getStringToSave() {
_getPrefItems();
return _stringToSave;
}
}
As you can see, there is a bool that is toggled in Page01 and a String that is displayed from a value that is generated and passed through from the buttons widget.
After looking at other tutorials on this matter, I think i have the change notifier set up correctly but am unsure about how to set up the main.dart and Page01 so that when the bool and String are set, they stay like that when the app is rebooted.
i'm currently getting an error in main.dart:
ChangeNotifierProvider(create: (_) => GlobalSettings1()),
asking me to add required argument prefs but i'm unsure what the code should be to go in the brackets.
thanks so much and any help would be greatly appreciated.
You need pass an instance of SharedPreferences to your GlobalSettings1, also change GlobalSettings1 to this:
class GlobalSettings1 with ChangeNotifier {
bool _toggleSwitch01 = true;
String _stringToSave = 'Choice01';
final SharedPreferences prefs;
GlobalSettings1({required this.prefs});
bool get toggleSwitch01 => _toggleSwitch01;
String get stringToSave => _stringToSave;
void ToggleSwitch() {
_toggleSwitch01 = !_toggleSwitch01;
_setPrefItems();
notifyListeners();
}
void StringToSave(val) {
_stringToSave = val;
_setPrefItems();
notifyListeners();
}
void _setPrefItems() {
prefs.setBool('toggleSwitch01', _toggleSwitch01);
prefs.setString('stringToSave', _stringToSave);
notifyListeners();
}
void _getPrefItems() { // <=== change this
_stringToSave = prefs.getString('stringToSave') ?? '';
_toggleSwitch01 = prefs.getBool('autoUpdateVariables') ?? true;
notifyListeners();
}
bool getToggleSwitch01() {
_getPrefItems();
return _toggleSwitch01;
}
String getStringToSave() {
_getPrefItems();
return _stringToSave;
}
}
then change your main to this:
void main() {
SharedPreferences prefs = await SharedPreferences.getInstance();
runApp(
MultiProvider(
providers: [
ChangeNotifierProvider(create: (_) => GlobalSettings1(prefs: prefs)),
ChangeNotifierProvider(create: (_) => SectionSettings1()),
],
child: MyApp(),
),
);
}
Currently, you are passing an instance on constructor.
GlobalSettings1({required this.prefs});
While it is SharedPreferences you can do get instance with
class GlobalSettings1 with ChangeNotifier {
bool _toggleSwitch01 = true;
String _stringToSave = 'Choice01';
GlobalSettings1();
......
void _setPrefItems() async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setBool('toggleSwitch01', _toggleSwitch01);
prefs.setString('stringToSave', _stringToSave);
notifyListeners();
}

flutter dart language code get current location of user and extract last firebase location and calculate remaining time and distance draw poly lines

i already make a flutter dart language through which i update my location to firebase i want make one more dart funtion on mymap.dart file which get current location of user and extract last firebase location and calculate remaining time and distance in widget and on map draw poly lines thanks
Here is my complete code with code you will understand all working of app
import 'dart:async';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:welcome_login/Login.dart';
import 'ui/mymap.dart';
import 'getstdloc.dart';
import 'package:location/location.dart' as loc;
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MaterialApp(home: GPSscreen()));
}
class GPSscreen extends StatefulWidget {
const GPSscreen({
Key? key,
}) : super(key: key);
#override
_GPSscreenState createState() => _GPSscreenState();
}
class _GPSscreenState extends State<GPSscreen> {
final loc.Location location = loc.Location();
StreamSubscription<loc.LocationData>? _locationSubscription;
#override
void initState() {
super.initState();
location.changeSettings(interval: 300, accuracy: loc.LocationAccuracy.high);
location.enableBackgroundMode(enable: true);
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Live UOH Buses Locations'),
),
body: Column(
children: [
TextButton(
onPressed: () {
_getLocation();
},
child: Text('add my location')),
TextButton(
onPressed: () {
_listenLocation();
},
child: Text('enable live location')),
TextButton(
onPressed: () {
_stopListening();
},
child: Text('stop live location')),
Expanded(
child: StreamBuilder(
stream:
FirebaseFirestore.instance.collection('location').snapshots(),
builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (!snapshot.hasData) {
return Center(child: CircularProgressIndicator());
}
return ListView.builder(
itemCount: snapshot.data?.docs.length,
itemBuilder: (context, index) {
return ListTile(
title:
Text(snapshot.data!.docs[index]['name'].toString()),
subtitle: Row(
children: [
Text(snapshot.data!.docs[index]['latitude']
.toString()),
SizedBox(
width: 20,
),
Text(snapshot.data!.docs[index]['longitude']
.toString()),
],
),
trailing: IconButton(
icon: Icon(Icons.directions),
onPressed: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) =>
MyMap(snapshot.data!.docs[index].id)));
},
),
);
});
},
)),
],
),
);
}
_getLocation() async {
try {
final loc.LocationData _locationResult = await location.getLocation();
await FirebaseFirestore.instance.collection('location').doc('user1').set({
'latitude': _locationResult.latitude,
'longitude': _locationResult.longitude,
'name': 'bus 1'
}, SetOptions(merge: true));
} catch (e) {
print(e);
}
}
Future<void> _listenLocation() async {
_locationSubscription = location.onLocationChanged.handleError((onError) {
print(onError);
_locationSubscription?.cancel();
setState(() {
_locationSubscription = null;
});
}).listen((loc.LocationData currentlocation) async {
await FirebaseFirestore.instance.collection('location').doc('user1').set({
'latitude': currentlocation.latitude,
'longitude': currentlocation.longitude,
'name': User,
}, SetOptions(merge: true));
});
}
_stopListening() {
_locationSubscription?.cancel();
setState(() {
_locationSubscription = null;
});
}
}
class MyMap extends StatefulWidget {
final String user_id;
MyMap(this.user_id);
#override
_MyMapState createState() => _MyMapState();
}
class _MyMapState extends State<MyMap> {
final loc.Location location = loc.Location();
late GoogleMapController _controller;
bool _added = false;
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Live UOH Buses Locations'),
),
body: StreamBuilder(
stream: FirebaseFirestore.instance.collection('location').snapshots(),
builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (_added) {
mymap(snapshot);
}
if (!snapshot.hasData) {
return Center(child: CircularProgressIndicator());
}
return GoogleMap(
mapType: MapType.normal,
markers: {
Marker(
position: LatLng(
snapshot.data!.docs.singleWhere((element) =>
element.id == widget.user_id)['latitude'],
snapshot.data!.docs.singleWhere((element) =>
element.id == widget.user_id)['longitude'],
),
markerId: MarkerId('id'),
icon: BitmapDescriptor.defaultMarker),
},
initialCameraPosition: CameraPosition(
target: LatLng(
snapshot.data!.docs.singleWhere(
(element) => element.id == widget.user_id)['latitude'],
snapshot.data!.docs.singleWhere(
(element) => element.id == widget.user_id)['longitude'],
),
zoom: 14.47),
onMapCreated: (GoogleMapController controller) async {
setState(() {
_controller = controller;
_added = true;
});
},
);
},
));
}
Future<void> mymap(AsyncSnapshot<QuerySnapshot> snapshot) async {
await _controller
.animateCamera(CameraUpdate.newCameraPosition(CameraPosition(
target: LatLng(
snapshot.data!.docs.singleWhere(
(element) => element.id == widget.user_id)['latitude'],
snapshot.data!.docs.singleWhere(
(element) => element.id == widget.user_id)['longitude'],
),
zoom: 14.47)));
}
}

how to make string as global variable in flutter

I was create SharedPreferences to save user loading in logon page. Then data of user will be save in SharedPreferences and move to main page. But my problem now in main page I need use this variable in different places in main page. But I cant do that.
I need to make variable of logindata can use in each places in main page I try to use in drawer to make logout. No I get error as:
Undefined name 'logindata'.
this is my code:
void initial() async {
logindata = await SharedPreferences.getInstance();
setState(() {
username = logindata.getString('username');
return username;
});
}
my full code:
import 'package:flutter/material.dart';
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:shared_preferences/shared_preferences.dart';
import 'addnewtopics.dart';
import 'DetilesOfMainPage.dart';
import 'loginpage.dart';
class MyApp extends StatelessWidget {
final String email;
MyApp({Key key, #required this.email}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('JSON ListView')
),
drawer: Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: <Widget>[
DrawerHeader(
child: Text('Drawer Header'),
decoration: BoxDecoration(
color: Colors.blue,
),
),
ListTile(
title: Text('Item 1'),
onTap: () {
logindata.setBool('login', true);// here I need to use It ========================
Navigator.pushReplacement(context,
new MaterialPageRoute(builder: (context) => LoginUser()));
Navigator.pop(context);
},
),
ListTile(
title: Text('Item 2'),
onTap: () {
// Navigator.pop(context);
},
),
],
),
),
body: JsonImageList(),
floatingActionButton: FloatingActionButton(
onPressed: () {
Navigator.push(context, MaterialPageRoute(builder: (context) => UploadImageDemo()
),);
},
child: Icon(Icons.add),
),
));
}
}
class Flowerdata {
int id;
String flowerName;
String flowerImageURL;
Flowerdata({
this.id,
this.flowerName,
this.flowerImageURL
});
factory Flowerdata.fromJson(Map<String, dynamic> json) {
return Flowerdata(
id: json['id'],
flowerName: json['nametopics'],
flowerImageURL: json['image']
);
}
}
class JsonImageList extends StatefulWidget {
JsonImageListWidget createState() => JsonImageListWidget();
}
class JsonImageListWidget extends State {
SharedPreferences logindata;
String username;
#override
void initState() {
// TODO: implement initState
super.initState();
initial();
}
void initial() async {
logindata = await SharedPreferences.getInstance();
setState(() {
username = logindata.getString('username');
return username;
});
}
final String apiURL = 'http://xxxxxxxxx/getFlowersList.php';
Future<List<Flowerdata>> fetchFlowers() async {
var response = await http.get(apiURL);
if (response.statusCode == 200) {
final items = json.decode(response.body).cast<Map<String, dynamic>>();
List<Flowerdata> listOfFruits = items.map<Flowerdata>((json) {
return Flowerdata.fromJson(json);
}).toList();
return listOfFruits;
}
else {
throw Exception('Failed to load data from Server.');
}
}
getItemAndNavigate(String item, BuildContext context){
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SecondScreen(itemHolder : item)
)
);
}
#override
Widget build(BuildContext context) {
return FutureBuilder<List<Flowerdata>>(
future: fetchFlowers(),
builder: (context, snapshot) {
if (!snapshot.hasData) return Center(
child: CircularProgressIndicator()
);
return ListView(
children: snapshot.data
.map((data) => Column(children: <Widget>[
GestureDetector(
onTap: ()=>{
getItemAndNavigate(data.flowerName, context)
},
child: Row(
children: [
Container(
width: 200,
height: 100,
margin: EdgeInsets.fromLTRB(10, 0, 10, 0),
child: ClipRRect(
borderRadius: BorderRadius.circular(8.0),
child:
Image.network(data.flowerImageURL,
width: 200, height: 100, fit: BoxFit.cover,))),
Flexible(child:
Text(data.flowerName,
style: TextStyle(fontSize: 18)))
]),),
Divider(color: Colors.black),
],))
.toList(),
);
},
);
}
}
Anyone know how can make that?
You need var keyword, in your case you can directly use
var logindata = await SharedPreferences.getInstance();
You do not need to make it global, because SharedPreferences.getInstance() is Singleton
Every time you use var logindata = await SharedPreferences.getInstance(); will get the same instance
Also there is no performance issue when you call getInstance(), because it's cached, you can see source code snippet below
class SharedPreferences {
SharedPreferences._(this._preferenceCache);
...
static Future<SharedPreferences> getInstance() async {
if (_completer == null) {
_completer = Completer<SharedPreferences>();
try {
final Map<String, Object> preferencesMap =
await _getSharedPreferencesMap();
_completer.complete(SharedPreferences._(preferencesMap));
} on Exception catch (e) {
// If there's an error, explicitly return the future with an error.
// then set the completer to null so we can retry.
_completer.completeError(e);
final Future<SharedPreferences> sharedPrefsFuture = _completer.future;
_completer = null;
return sharedPrefsFuture;
}
}
return _completer.future;
When you declare a String outside of class and does not contain _ before variable name like _localString it become global
String globalString = ""; //global, import can be seen
String _localString = ""; //local and can only be seen in this file, import can not seen
void main() async{
var logindata = await SharedPreferences.getInstance();
runApp(MyApp());
}
You simply need to put your variable outside of any class or method. An example is to create a file globals.dart then put all your globals in it and import the file when you need.
Example
// globals.dart
String globalString;
int globalInt;
bool globalBool;
// in any other file
import 'globals.dart' as globals;
globals.globalString = "Global String";

How To Pass Information From one Screen to another Screen

I wanted to Download a Image with its progress and message. I wanted to show it in a dialog. When ever I click Download Button the Image gets downloaded and the Container pops up, but it does not Show any value.
The below code uses Image_downloader package. Raised button downloads the image and display the Blank Container without any value;
import 'dart:async';
import 'dart:io';
import 'Download.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:image_downloader/image_downloader.dart';
void main() => runApp(HomePage());
class HomePage extends StatefulWidget {
#override
HomePageState createState() => HomePageState();
}
class HomePageState extends State<HomePage> {
String message = "";
String path = "";
int _progress = 0;
#override
void initState() {
super.initState();
ImageDownloader.callback(onProgressUpdate: (String imageId, int progress) {
setState(() {
_progress = progress;
});
});
}
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
RaisedButton(
onPressed: () {
_downloadImage(
"https://images.unsplash.com/photo-1503023345310-bd7c1de61c7d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80",
);
showDialog(
context: context,
builder: (_) => FunkyOverlay(progress: _progress, message: message,),
);
},
child: Text("default destination"),
),
],
),
),
),
);
}
Future<void> _downloadImage(String url,
{AndroidDestinationType destination, bool whenError = false}) async {
String fileName;
String path;
try {
String imageId;
if (whenError) {
imageId = await ImageDownloader.downloadImage(url).catchError((error) {
if (error is PlatformException) {
var path = "";
if (error.code == "404") {
print("Not Found Error.");
} else if (error.code == "unsupported_file") {
print("UnSupported FIle Error.");
path = error.details["unsupported_file_path"];
}
setState(() {
message = error.toString();
path = path;
});
}
print(error);
}).timeout(Duration(seconds: 10), onTimeout: () {
print("timeout");
});
} else {
if (destination == null) {
imageId = await ImageDownloader.downloadImage(url);
} else {
imageId = await ImageDownloader.downloadImage(
url,
destination: destination,
);
}
}
if (imageId == null) {
return;
}
fileName = await ImageDownloader.findName(imageId);
path = await ImageDownloader.findPath(imageId);
} on PlatformException catch (error) {
setState(() {
message = error.message;
});
return;
}
if (!mounted) return;
setState(() {
message = 'Image Downloaded';
});
}
}
This is the Pop up Container Part
import 'package:flutter/material.dart';
class FunkyOverlay extends StatefulWidget {
String message;
int progress;
FunkyOverlay({#required this.message, #required this.progress});
#override
State<StatefulWidget> createState() => FunkyOverlayState(message, progress);
}
class FunkyOverlayState extends State<FunkyOverlay>
with SingleTickerProviderStateMixin {
String message;
int progress;
FunkyOverlayState(this.message, this.progress);
AnimationController controller;
Animation<double> scaleAnimation;
#override
void initState() {
super.initState();
controller =
AnimationController(vsync: this, duration: Duration(milliseconds: 450));
scaleAnimation =
CurvedAnimation(parent: controller, curve: Curves.elasticInOut);
controller.addListener(() {
setState(() {});
});
controller.forward();
}
#override
Widget build(BuildContext context) {
return Center(
child: Material(
color: Colors.transparent,
child: ScaleTransition(
scale: scaleAnimation,
child: Container(
decoration: ShapeDecoration(
color: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15.0),
),
),
child: Padding(
padding: const EdgeInsets.all(50.0),
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text('Downloaded: $progress'),
Text(message)
],
),
),
),
),
),
);
}
}
You can copy paste run full code below
You can use StreamBuilder to receive progress from onProgressUpdate
class HomePageState extends State<HomePage> {
...
#override
void initState() {
super.initState();
_events = new StreamController<int>.broadcast();;
_events.add(0);
ImageDownloader.callback(onProgressUpdate: (String imageId, int progress) {
setState(() {
print("progress $progress");
_progress = progress;
_events.add(progress);
});
return StreamBuilder<int>(
stream: _events.stream,
builder: (BuildContext context, AsyncSnapshot<int> snapshot) {
return Center(
...
children: <Widget>[
Text('Downloaded: ${snapshot.data.toString()}'),
Text(message)
working demo
full code
import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:io';
import 'package:flutter/services.dart';
import 'package:image_downloader/image_downloader.dart';
void main() => runApp(MaterialApp(home: HomePage()));
class HomePage extends StatefulWidget {
#override
HomePageState createState() => HomePageState();
}
StreamController<int> _events;
class HomePageState extends State<HomePage> {
String message = "";
String path = "";
int _progress = 0;
#override
void initState() {
super.initState();
_events = new StreamController<int>.broadcast();
;
_events.add(0);
ImageDownloader.callback(onProgressUpdate: (String imageId, int progress) {
setState(() {
print("progress $progress");
_progress = progress;
_events.add(progress);
if (progress == 100) {
Navigator.pop(context);
}
});
});
}
#override
void dispose() {
// TODO: implement dispose
_events.close();
super.dispose();
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
RaisedButton(
onPressed: () {
_events.add(0);
_downloadImage(
"https://images.unsplash.com/photo-1503023345310-bd7c1de61c7d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80",
);
showDialog(
context: context,
builder: (_) => FunkyOverlay(
progress: _progress,
message: message,
),
);
},
child: Text("default destination"),
),
],
),
),
);
}
Future<void> _downloadImage(String url,
{AndroidDestinationType destination, bool whenError = false}) async {
String fileName;
String path;
try {
String imageId;
if (whenError) {
imageId = await ImageDownloader.downloadImage(url).catchError((error) {
if (error is PlatformException) {
var path = "";
if (error.code == "404") {
print("Not Found Error.");
} else if (error.code == "unsupported_file") {
print("UnSupported FIle Error.");
path = error.details["unsupported_file_path"];
}
setState(() {
message = error.toString();
path = path;
});
}
print(error);
}).timeout(Duration(seconds: 10), onTimeout: () {
print("timeout");
});
} else {
if (destination == null) {
imageId = await ImageDownloader.downloadImage(url);
} else {
imageId = await ImageDownloader.downloadImage(
url,
destination: destination,
);
}
}
if (imageId == null) {
print("imageId is null");
return;
}
fileName = await ImageDownloader.findName(imageId);
path = await ImageDownloader.findPath(imageId);
} on PlatformException catch (error) {
setState(() {
message = error.message;
});
return;
}
if (!mounted) return;
setState(() {
message = 'Image Downloaded';
});
}
}
class FunkyOverlay extends StatefulWidget {
String message;
int progress;
FunkyOverlay({#required this.message, #required this.progress});
#override
State<StatefulWidget> createState() => FunkyOverlayState(message, progress);
}
class FunkyOverlayState extends State<FunkyOverlay>
with SingleTickerProviderStateMixin {
String message;
int progress;
FunkyOverlayState(this.message, this.progress);
AnimationController controller;
Animation<double> scaleAnimation;
#override
void initState() {
super.initState();
controller =
AnimationController(vsync: this, duration: Duration(milliseconds: 450));
scaleAnimation =
CurvedAnimation(parent: controller, curve: Curves.elasticInOut);
controller.addListener(() {
setState(() {});
});
controller.forward();
}
#override
Widget build(BuildContext context) {
print("StreamBuilder build");
return StreamBuilder<int>(
stream: _events.stream,
builder: (BuildContext context, AsyncSnapshot<int> snapshot) {
print("snapshot.data ${snapshot.data.toString()}");
return Center(
child: Material(
color: Colors.transparent,
child: ScaleTransition(
scale: scaleAnimation,
child: Container(
decoration: ShapeDecoration(
color: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15.0),
),
),
child: Padding(
padding: const EdgeInsets.all(50.0),
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text('Downloaded: ${snapshot.data.toString()}'),
Text(message)
],
),
),
),
),
),
);
});
}
}