I have a Flutter app which in home screen I use google map. At the begin, I call Firebase to take the places which I have already input. Then the user can add a new place if double in the map this lead him to SecondScreen. The user input title, story, and photo. I have and another screen where the user can view info for existing marker where can also delete it. My problem is when user input or delete marker and come back to the home page, in the map, the places are not refreshed and I see that the Firebase is not called again. How can I solve this problem? The Firebase is called in MapsDemo class
My code:
import 'dart:ui' as ui;
import 'dart:io';
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:geolocator/geolocator.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:image_picker/image_picker.dart';
import 'package:uuid/uuid.dart';
import 'infopage.dart';
void main() {
GoogleMapController.init();
final size = MediaQueryData.fromWindow(ui.window).size;
final GoogleMapOverlayController controller =
GoogleMapOverlayController.fromSize(
width: size.width,
height: size.height,
);
final mapController = controller.mapController;
//// Set firebase https://www.youtube.com/watch?v=DqJ_KjFzL9I
//// TODO check if every time I come back the above commands call
// Firestore.instance.collection('Stories').snapshots().listen((data) =>
// data.documents.forEach((doc) =>
////Read all the markers from firebase and add them to map
//
// AddMarkers(mapController, ConvertCoordinates(doc["lat"], doc["lng"]),
// doc["title"], doc["story"], doc["url"])));
final Widget mapWidget = GoogleMapOverlay(controller: controller);
runApp(
MaterialApp(
home: new Scaffold(
appBar: AppBar(
title: TextField(
decoration: InputDecoration.collapsed(hintText: 'Search'),
),
actions: <Widget>[
IconButton(
icon: const Icon(Icons.my_location),
onPressed: () async {
Position position = await Geolocator()
.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
// Get my current position
final location = LatLng(position.latitude, position.longitude);
mapController.markers.clear();
mapController.addMarker(MarkerOptions(
position: location,
infoWindowText: InfoWindowText("Here you are!", "Add me"),
visible: true));
mapController.animateCamera(
CameraUpdate.newLatLngZoom(location, 20.0),
);
},
),
],
),
body: MapsDemo(mapWidget, controller.mapController),
floatingActionButton: FloatingActionButton(
onPressed: () {},
child: const Icon(Icons.my_location),
),
),
navigatorObservers: <NavigatorObserver>[controller.overlayController],
),
);
}
class MapsDemo extends StatelessWidget {
MapsDemo(this.mapWidget, this.controller);
final Widget mapWidget;
final GoogleMapController controller;
#override
Widget build(BuildContext context) {
// Set firebase https://www.youtube.com/watch?v=DqJ_KjFzL9I
// TODO check if every time I come back the above commands call
Firestore.instance.collection('Stories').snapshots().listen((data) =>
data.documents.forEach((doc) =>
//Read all the markers from firebase and add them to map
AddMarkers(controller, ConvertCoordinates(doc["lat"], doc["lng"]),
doc["title"], doc["story"], doc["url"])));
controller.onMarkerTapped.add((Marker marker) async {
//Marker listener open new page info page
String titlos = marker.options.infoWindowText.title;
if (titlos != "Here you are!") {
String story = marker.options.infoWindowText.snippet.split("?")[0];
String url = marker.options.infoWindowText.snippet.split("?")[1];
String realUrl = await makeRequest(url);
String downloadUrl =getDownloadUrl(realUrl,url);
Navigator.push(
// Parse title to next page/screen
context,
new MaterialPageRoute(
builder: (context) =>
new AboutPage(title: titlos, story: story, url: downloadUrl,)));
} else {
Navigator.push(
// Parse title to next page/screen
context,
new MaterialPageRoute(builder: (context) => new SecondScreen(
marker.options.position.latitude.toString(),
marker.options.position.longitude.toString())));
}
});
return Center(child: mapWidget);
}
}
LatLng ConvertCoordinates(String lat, String lng) {
// Convert strings coordinates to LatLng
return LatLng(double.parse(lat), double.parse(lng));
}
void AddMarkers(GoogleMapController map, LatLng coor, String title,
String story, String url) {
map.addMarker(MarkerOptions(
position: coor, infoWindowText: InfoWindowText(title, '$story'+'?'+'$url')));
}
Future<String> makeRequest(String n) async {
String baseUrl = 'https://firebasestorage.googleapis.com/v0/b/storymap-da000.appspot.com/o/';
String url = '$baseUrl' + '$n';
var client = new http.Client();
final response = await client.get(url);
return response.body;
}
String getDownloadUrl(String url, String name){
String baseUrl = 'https://firebasestorage.googleapis.com/v0/b/storymap-da000.appspot.com/o/';
String token = url.replaceAll("{", "").replaceAll("}", "").split('"downloadTokens": ')[1].replaceAll('"', '');
return '$baseUrl'+'$name'+"?alt=media&token="+'$token';
}
class SecondScreen extends StatelessWidget {
final String lat;
final String lng;
// final String image_name;
final titleC = TextEditingController();
final storyC = TextEditingController();
static final String image_name = Uuid().v1();
SecondScreen(this.lat, this.lng,);
Future getImage() async {
var image = await ImagePicker.pickImage(source: ImageSource.camera);
final StorageReference firebaseStorageRef =
FirebaseStorage.instance.ref().child(image_name);
final StorageUploadTask task =
firebaseStorageRef.putFile(image);
}
uploadFirebase(){
var map= {
"title":titleC.text,
"story":storyC.text,
"url":image_name,
"lat":lat,
"lng":lng
};
Firestore.instance.collection('Stories').document()
.setData(map);
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Second Screen"),
),
body: Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new TextField (
controller: titleC,
),
new TextField(
controller: storyC,
),
new RaisedButton(
onPressed: () => getImage(),
child: new Text('Take photo'),
),
new RaisedButton(
onPressed: () => uploadFirebase(),
child: new Text('Confirm'),
)
])
),
);
}
}
[Edit]
I change my code to this,
import 'dart:ui' as ui;
import 'dart:io';
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:geolocator/geolocator.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:image_picker/image_picker.dart';
import 'package:uuid/uuid.dart';
import 'infopage.dart';
void main() {
GoogleMapController.init();
final size = MediaQueryData.fromWindow(ui.window).size;
final GoogleMapOverlayController controller =
GoogleMapOverlayController.fromSize(
width: size.width,
height: size.height,
);
final mapController = controller.mapController;
//// Set firebase https://www.youtube.com/watch?v=DqJ_KjFzL9I
//// TODO check if every time I come back the above commands call
// Firestore.instance.collection('Stories').snapshots().listen((data) =>
// data.documents.forEach((doc) =>
////Read all the markers from firebase and add them to map
//
// AddMarkers(mapController, ConvertCoordinates(doc["lat"], doc["lng"]),
// doc["title"], doc["story"], doc["url"])));
final Widget mapWidget = GoogleMapOverlay(controller: controller);
runApp(
MaterialApp(
home: new Scaffold(
appBar: AppBar(
title: TextField(
decoration: InputDecoration.collapsed(hintText: 'Search'),
),
actions: <Widget>[
IconButton(
icon: const Icon(Icons.my_location),
onPressed: () async {
Position position = await Geolocator()
.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
// Get my current position
final location = LatLng(position.latitude, position.longitude);
mapController.markers.clear();
mapController.addMarker(MarkerOptions(
position: location,
infoWindowText: InfoWindowText("Here you are!", "Add me"),
visible: true,draggable: true));
mapController.animateCamera(
CameraUpdate.newLatLngZoom(location, 20.0),
);
},
),
IconButton(
icon: const Icon(Icons.refresh),
onPressed: () {RefreshIt(mapController);},
)
],
),
body: _MapDemo(mapWidget:mapWidget, controller:controller.mapController),
),
navigatorObservers: <NavigatorObserver>[controller.overlayController],
),
);
}
class _MapDemo extends StatefulWidget{
final Widget mapWidget;
final GoogleMapController controller;
const _MapDemo({Key key, this.mapWidget, this.controller}) : super(key: key);
#override
MapsDemo createState() => MapsDemo(this.mapWidget, this.controller);
}
class MapsDemo extends State<_MapDemo> {
MapsDemo(this.mapWidget, this.controller);
final Widget mapWidget;
final GoogleMapController controller;
#override
Widget build(BuildContext context) {
// Set firebase https://www.youtube.com/watch?v=DqJ_KjFzL9I
controller.markers.clear();
// TODO check if every time I come back the above commands call
Firestore.instance.collection('Stories').snapshots().listen((data) =>
data.documents.forEach((doc) =>
//Read all the markers from firebase and add them to map
AddMarkers(controller, ConvertCoordinates(doc["lat"], doc["lng"]),
doc["title"], doc["story"], doc["url"])));
controller.onMarkerTapped.add((Marker marker) async {
//Marker listener open new page info page
String titlos = marker.options.infoWindowText.title;
if (titlos != "Here you are!") {
String story = marker.options.infoWindowText.snippet.split("?")[0];
String url = marker.options.infoWindowText.snippet.split("?")[1];
String realUrl = await makeRequest(url);
String downloadUrl =getDownloadUrl(realUrl,url);
Navigator.push(
// Parse title to next page/screen
context,
new MaterialPageRoute(
builder: (context) =>
new AboutPage(title: titlos, story: story, url: downloadUrl,)));
} else {
Navigator.push(
// Parse title to next page/screen
context,
new MaterialPageRoute(builder: (context) => new _SecondScreen(
lat:marker.options.position.latitude.toString(),
lng:marker.options.position.longitude.toString())));
}
});
return Center(child: mapWidget);
}
}
LatLng ConvertCoordinates(String lat, String lng) {
// Convert strings coordinates to LatLng
return LatLng(double.parse(lat), double.parse(lng));
}
void AddMarkers(GoogleMapController map, LatLng coor, String title,
String story, String url) {
map.addMarker(MarkerOptions(
position: coor, infoWindowText: InfoWindowText(title, '$story'+'?'+'$url')));
}
void RefreshIt(GoogleMapController mapController){
//TODO Refresh the map via this float button
}
Future<String> makeRequest(String n) async {
String baseUrl = 'https://firebasestorage.googleapis.com/v0/b/storymap-da000.appspot.com/o/';
String url = '$baseUrl' + '$n';
var client = new http.Client();
final response = await client.get(url);
return response.body;
}
String getDownloadUrl(String url, String name){
String baseUrl = 'https://firebasestorage.googleapis.com/v0/b/storymap-da000.appspot.com/o/';
String token = url.replaceAll("{", "").replaceAll("}", "").split('"downloadTokens": ')[1].replaceAll('"', '');
return '$baseUrl'+'$name'+"?alt=media&token="+'$token';
}
class _SecondScreen extends StatefulWidget{
final String lat;
final String lng;
const _SecondScreen({Key key, this.lat, this.lng}) : super(key: key);
#override
SecondScreen createState() => SecondScreen(this.lat, this.lng);
}
class SecondScreen extends State<_SecondScreen> {
final String lat;
final String lng;
// final String image_name;
final titleC = TextEditingController();
final storyC = TextEditingController();
static final String image_name = Uuid().v1();
SecondScreen(this.lat, this.lng,);
Future getImage() async {
var image = await ImagePicker.pickImage(source: ImageSource.camera);
final StorageReference firebaseStorageRef =
FirebaseStorage.instance.ref().child(image_name);
final StorageUploadTask task =
firebaseStorageRef.putFile(image);
}
uploadFirebase(){
var map= {
"title":titleC.text,
"story":storyC.text,
"url":image_name,
"lat":lat,
"lng":lng
};
Firestore.instance.collection('Stories').document()
.setData(map);
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Input Screen"),
),
body: Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new TextField (
controller: titleC,
),
new TextField(
controller: storyC,
),
new RaisedButton(
onPressed: () => getImage(),
child: new Text('Take photo'),
),
new RaisedButton(
onPressed: () => uploadFirebase(),
child: new Text('Confirm'),
)
])
),
);
}
}
My firebase is shared with a website with the same concept, when I add something in the website the new marker is appeared in my Flutter app, but not when I delete a marker. On the other hand, when I add or remove marker from my Flutter app I don't get any refresh in my map... Any idea?
I set the mapStyle after resuming app from background and it worked for me:
mapController.setMapStyle("[]");
All of your widgets are Stateless which means they won't be able to update or change upon user interaction. This tutorial from the Flutter documentation goes through how to add interactivity to your app using Stateful widgets which will solve your problem.
Related
Hello Im very to the flutter framework, so please let me know if im going wrong anywhere and the appropriate way of doing the things.
this is a drawerPage.dar file
In this file im trying to call a function getData for retrieving the data from firebase,this fucntion is in Database.dart file.
Database.dart
In the Database.dart file i wrote the getData function inside which im retrieving a particular record from the firebase and storing in a global variable. And then im trying to print the global variable in the drawerPage.dart file.But here when ever i run the program, for the first time the variable is having a null value and upon hot reload the actual value is getting stored in the variable.Please let me know how can i get rid of this problem.
output
drawerPageOutput
drawerPage.dart
import 'package:attendee/constants.dart';
import 'package:attendee/models/userdeails.dart';
import 'package:attendee/pages/profile.dart';
import 'package:attendee/services/authentication_service.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:attendee/services/database.dart';
import 'package:provider/provider.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:attendee/pages/userdetails.dart';
class StudentDashboard extends StatefulWidget {
#override
_StudentDashboardState createState() => _StudentDashboardState();
}
class _StudentDashboardState extends State<StudentDashboard> {
userdetails userdetail;
final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
final AuthenticationService _auth = AuthenticationService();
#override
void initState() {
super.initState();
}
#override
Widget build(BuildContext context) {
DatabaseService().getData('email');
final drawerHeader = UserAccountsDrawerHeader(
accountName: Text(userName),
accountEmail: Text('${result}'),
currentAccountPicture
: CircleAvatar(
child: FlutterLogo(size: 42.0),
backgroundColor: Colors.white,
);
final drawerItems = ListView(
children: <Widget>[
drawerHeader,
ListTile(
title: Row(
children: <Widget>[
Icon(Icons.perm_identity_outlined),
Text(' Profile'),
],
),
onTap: () => Navigator.of(context).push(MaterialPageRoute(builder: (context)=>Profile())),
),
ListTile(
title: Text('To page 2'),
onTap: () => Navigator.of(context).push(_NewPage(2)),
),
ListTile(
title:Row(
children: <Widget>[
Icon(Icons.exit_to_app_rounded),
Text(' Logout'),
],
),
onTap: () async {
await _auth.signOut();
Navigator.of(context).pushNamed('/homepage');
},
),
],
);
return StreamProvider<List<userdetails>>.value(
value: DatabaseService().students,
initialData: [],
child: SafeArea(
child: Scaffold(
appBar: AppBar(
backgroundColor: Colors.lightGreen,
title: Text('Student Welcome'),
actions: <Widget>[
TextButton.icon(
onPressed: () async {
await _auth.signOut();
Navigator.of(context).pushNamed('/homepage');
},
icon: Icon(Icons.person),
label: Text('Logout'))
],
),
body:
UserDetails(),
drawer: GestureDetector(
onTap: display,
child: Drawer(
child: drawerItems,
),
),
),
),
);
}
display() async{
await DatabaseService().getData('email');
}
}
// <Null> means this route returns nothing.
class _NewPage extends MaterialPageRoute<Null> {
_NewPage(int id)
: super(builder: (BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Page $id'),
elevation: 1.0,
),
body: Center(
child: Text('Page $id'),
),
);
});
}
database.dart
import 'package:attendee/models/userdeails.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:flutter_icons/flutter_icons.dart';
import '../constants.dart';
class DatabaseService{
final String uid;
DatabaseService({this.uid});
//collection reference
final CollectionReference user_details=FirebaseFirestore.instance.collection('users');`
final CollectionReference tutor_details` `=FirebaseFirestore.instance.collection("tutors");`
Future updateStudentData(String fullname,String mobilenumber,String `email,String rollno,String tutorid,String role) async {`
return await user_details.doc(uid).set({
'fullname' : fullname,
'mobilenumber': mobilenumber,
'email' : email,
'rollno': rollno,
'tutorid': tutorid,
'role' : role,//FMERT series
});
}
Future updateTutorData(String fullname,String mobilenumber,String `email,String rollno,String tutorid,String role) async {`
return await tutor_details.doc(uid).set({
'fullname' : fullname,
'mobilenumber': mobilenumber,
'email' : email,
'rollno': rollno,
'tutorid': tutorid,
'role' : role,//FMERT series
});
}
//studentDetails from snapshot
List<userdetails> _studentDetailsFromSnapshot(QuerySnapshot snapshot){
return snapshot.docs.map((doc){
return userdetails(
fullname: doc.data()['fullname'] ?? '',
mobilenumber: doc.data()['mobilenumber'] ?? '',
email: doc.data()['email'] ?? '',
rollno: doc.data()['rollno'] ?? '',
tutorid: doc.data()['tutorid'] ?? '',
//role: doc.data()['role'] ?? '',
);
}).toList();
}
//get students stream
Stream<List<userdetails>> get students {
return user_details.snapshots()
.map(_studentDetailsFromSnapshot);
}
//tutorsDetails from snapshot
List<userdetails> _tutorDetailsFromSnapshot(QuerySnapshot snapshot){
return snapshot.docs.map((doc){
return userdetails(
fullname: doc.data()['fullname'] ?? '',
mobilenumber: doc.data()['mobilenumber'] ?? '',
email: doc.data()['email'] ?? '',
rollno: doc.data()['rollno'] ?? '',
tutorid: doc.data()['tutorid'] ?? '',
);
}).toList();
}
//get tutors stream
Stream<List<userdetails>> get tutors {
return user_details.snapshots()
.map(_studentDetailsFromSnapshot);
}
void display() {
tutor_details.get().then((querySnapshot) {
querySnapshot.docs.forEach((result) {
print(result.data());
});
});
}
getData (String string) async{
String userId = await FirebaseAuth.instance.currentUser.uid;
final document = isTutor ? `FirebaseFirestore.instance.doc('tutors/$userId') :`
await FirebaseFirestore.instance.doc('users/$userId');
document.get().then((DocumentSnapshot) async {
if(string =='role') {
checkRole = DocumentSnapshot.data()[string].toString();
print('$checkRole inside getData Function');
//return checkRole;
print(checkRole);
}
else {
print(result);
result = await DocumentSnapshot.data()[string].toString();
print('${DocumentSnapshot.data()[string].toString()} in the `database else block');`
//return result;
}
//print(document("name"));
});
}
}
After changes
terminaloutput
draweroutput
""when ever i run the program, for the first time the variable is having a null value and upon hot reload the actual value is getting stored in the variable""
When we try to get data from http / https request, it takes some time. Meanwhile the page gets loaded and you get null values.
You can use Provider package to resolve this issue, or try the below code. Please add the below code in your drawerPage.dart.
What I have done below is made getData() return type. Only on receiving a value from this function, _loadOnce will change to false & final screen will be shown.
Database.dart
Future<bool> getData (String string) async{
String userId = await FirebaseAuth.instance.currentUser.uid;
final document = isTutor ? `FirebaseFirestore.instance.doc('tutors/$userId') :`
await FirebaseFirestore.instance.doc('users/$userId');
document.get().then((DocumentSnapshot) async {
if(string =='role') {
checkRole = DocumentSnapshot.data()[string].toString();
print('$checkRole inside getData Function');
//return checkRole;
print(checkRole);
return true;
}
else {
print(result);
result = await DocumentSnapshot.data()[string].toString();
print('${DocumentSnapshot.data()[string].toString()} in the `database else block');`
//return result;
return false;
}
//print(document("name"));
});
}
}
/// create a new variable.
bool _loadOnce = true;
/// shift your code `DatabaseService().getData('email');`
#override
void didChangeDependencies() {
if(_loadOnce == true) {
DatabaseService().getData('email').then((value) {
if(value == true){
setState(() {
_loadOnce = false;
});
} else {
/// you can write your code here
setState(() {
_loadOnce = false;
});
}
)}
}
super.didChangeDependencies();
}
Below code will show a spinner till the time all the code gets executed and values are retreived.
/// in your main page under Scaffold
body: _loadOnce == true
? Center(
child: CircularProgressIndicator(
backgroundColor: Theme.of(context).primaryColor,
),
)
: UserDetails(),
I'm currently stuck at where I'm trying to upload image to Firestore Storage and automatically store the image URL to Cloud Firestore. I have tried to manually upload the image to Firebase Storage and pasting the image url to Cloud Firestore then retrieving it to show the image in my app and it works. Heres the coding that I have done so far:
models/enter.dart
class Enter {
final String enterId;
final String enter;
final String price;
final String url;
Enter({this.enter, this.price, #required this.enterId, this.url});
factory Enter.fromJson(Map<String, dynamic> json){
return Enter(
enter: json['enter'],
price: json['price'],
url: json['url'],
enterId: json['enterId']
);
}
Map<String,dynamic> toMap(){
return {
'enter':enter,
'price':price,
'url':url,
'enterId':enterId
};
}
}
services/firestore_service2
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:journal_app/src/models/enter.dart';
class FirestoreService2 {
FirebaseFirestore _db = FirebaseFirestore.instance;
//Get Entries
Stream<List<Enter>> getEnter(){
return _db
.collection('enters')
.snapshots()
.map((snapshot) => snapshot.docs
.map((doc) => Enter.fromJson(doc.data()))
.toList());
}
//Upsert
Future<void> setEnter(Enter enter){
var options = SetOptions(merge:true);
return _db
.collection('enters')
.doc(enter.enterId)
.set(enter.toMap(),options);
}
//Delete
Future<void> removeEnter(String enterId){
return _db
.collection('enters')
.doc(enterId)
.delete();
}
}
provider/enter_provider.dart
import 'package:flutter/material.dart';
import 'package:journal_app/src/models/enter.dart';
import 'package:journal_app/src/services/firestore_service2.dart';
import 'package:uuid/uuid.dart';
class EnterProvider with ChangeNotifier {
final firestoreService = FirestoreService2();
String _enter;
String _price;
String _enterId;
String _url;
var uuid = Uuid();
//Getters
String get enter => _enter;
String get price => _price;
String get url => _url;
Stream<List<Enter>> get enters => firestoreService.getEnter();
//Setters
set changeEnter(String enter){
_enter = enter;
notifyListeners();
}
set changePrice(String price){
_price = price;
notifyListeners();
}
set changeUrl(String url){
_url = url;
notifyListeners();
}
//Functions
loadAll(Enter enter){
if (enter != null && price != null){
_enter =enter.enter;
_price =enter.price;
_url=enter.url;
_enterId = enter.enterId;
} else {
_enter = null;
_price = null;
_url = null;
_enterId = null;
}
}
saveEnter(){
if (_enterId == null){
//Add
var newEnter = Enter(enter: _enter, price: _price, url: _url, enterId: uuid.v1());
print(newEnter.enter);
print(newEnter.price);
print(newEnter.url);
firestoreService.setEnter(newEnter);
} else {
//Edit
var updatedEnter = Enter(enter: _enter, price: _price, url: _url, enterId: _enterId);
firestoreService.setEnter(updatedEnter);
}
}
removeEnter(String enterId){
firestoreService.removeEnter(enterId);
}
}
screens/enter.dart where user insert product name, price and image
import 'package:date_format/date_format.dart';
import 'package:flutter/material.dart';
import 'package:journal_app/src/models/enter.dart';
import 'package:journal_app/src/providers/enter_provider.dart';
import 'package:provider/provider.dart';
class EnterScreen extends StatefulWidget {
final Enter enter;
final Enter price;
final Enter category;
EnterScreen({this.enter, this.price, this.category});
#override
_EnterScreenState createState() => _EnterScreenState();
}
class _EnterScreenState extends State<EnterScreen> {
final enterController = TextEditingController();
final enterController2 = TextEditingController();
#override
void dispose() {
enterController.dispose();
enterController2.dispose();
super.dispose();
}
#override
void initState() {
final enterProvider = Provider.of<EnterProvider>(context,listen: false);
if (widget.enter != null){
//Edit
enterController.text = widget.enter.enter;
enterController2.text = widget.enter.price;
enterProvider.loadAll(widget.enter);
enterProvider.loadAll(widget.price);
} else {
//Add
enterProvider.loadAll(null);
}
super.initState();
}
#override
Widget build(BuildContext context) {
final enterProvider = Provider.of<EnterProvider>(context);
return Scaffold(
appBar: AppBar(title: Text('Products')),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: ListView(
children: [
TextField(
decoration: InputDecoration(
labelText: 'Product Name', border: InputBorder.none,
),
style: TextStyle(color: Colors.black, fontSize: 25),
maxLines: 5,
minLines: 2,
onChanged: (String value) => enterProvider.changeEnter = value,
controller: enterController,
),
TextField(
decoration: InputDecoration(
labelText: 'Product Price', border: InputBorder.none,
),
style: TextStyle(color: Colors.black, fontSize: 25),
maxLines: 5,
minLines: 2,
onChanged: (String value) => enterProvider.changePrice = value,
controller: enterController2,
),
RaisedButton(
color: Theme.of(context).accentColor,
child: Text('Save',style: TextStyle(color: Colors.white, fontSize: 20)),
onPressed: () {
enterProvider.saveEnter();
Navigator.of(context).pop();
},
),
(widget.enter != null) ? RaisedButton(
color: Colors.red,
child: Text('Delete',style: TextStyle(color: Colors.white, fontSize: 20)),
onPressed: () {
enterProvider.removeEnter(widget.enter.enterId);
Navigator.of(context).pop();
},
): Container(),
],
),
),
);
}
}
screens/product.dart where this screen show the product name, price and image in listview
import 'package:date_format/date_format.dart';
import 'package:flutter/material.dart';
import 'package:journal_app/src/models/enter.dart';
import 'package:journal_app/src/providers/enter_provider.dart';
import 'package:journal_app/src/screens/enter.dart';
import 'package:provider/provider.dart';
class ProductScreen extends StatelessWidget {
#override
Widget build(BuildContext context) {
final enterProvider = Provider.of<EnterProvider>(context);
return Scaffold(
appBar: AppBar(
title: Text('Products'),
),
body: StreamBuilder<List<Enter>>(
stream: enterProvider.enters,
builder: (context, snapshot) {
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (context, index) {
return ListTile(
leading: Image.network(
snapshot.data[index].url,
width: 100,
height: 100,
fit: BoxFit.fitWidth,
),
trailing:
Icon(Icons.edit, color: Theme.of(context).accentColor),
title: Text(
snapshot.data[index].enter, style: TextStyle(fontSize: 25),
),
subtitle: Text(
snapshot.data[index].price,
),
onTap: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) =>
EnterScreen(enter: snapshot.data[index], price: snapshot.data[index])));
},
);
});
}),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: () {
Navigator.of(context)
.push(MaterialPageRoute(builder: (context) => EnterScreen()));
},
),
);
}
}
The goal is to upload image to Firebase Storage, storing the image url at the same time when uploading new image.
I need advice for this project.
In order to upload image to Firebase Storage & save it in Cloud Firestore, you need to do the following:
Step 1: Get the image that you want to upload
For this, you can use the image_picker package
It will give you a PickedFile object (let's say it pickedFile). Convert this object to a File object by using this code:
final file = File(pickedFile.path);
Step 2: Upload this file to Firebase Storage:
Give the image that you want to upload a unique name.
final imageName = '${DateTime.now().millisecondsSinceEpoch}.png';
Create Firebase Storage Reference:
final firebaseStorageRef = FirebaseStorage.instance
.ref()
.child('images/$imageName'); // This will create a images directory in Firebase storage & save your image in that directory
Start uploading image:
final uploadTask = firebaseStorageRef.putFile(file);
final taskSnapshot = await uploadTask.onComplete;
Get the image URL:
final _fileURL = await taskSnapshot.ref.getDownloadURL();
Save this image URL in Cloud Firestore.
// This will save the image in "images" collection & update the "uploadedImage" value of document with id the same as the value of "id" variable.
await FirebaseFirestore.instance.collection(images).doc(id).update({'uploadedImage': _fileURL});
If you are updating an existing object then use the update method or else you can use the set method.
In your case you must follow bellow steps
First upload your image to Firebase Storage and get the download URL
Now you have the download URL and you can Upload your Enter object to Cloud FireStore with the url
Bellow method shows how could you store the image and get the download url list
Future<String> uploadImage(var imageFile ) async {
StorageReference ref = storage.ref().child("/photo.jpg");
StorageUploadTask uploadTask = ref.putFile(imageFile);
var dowurl = await (await uploadTask.onComplete).ref.getDownloadURL();
url = dowurl.toString();
return url;
}
Patients model dart file;
import 'package:flutter/material.dart';
class Patients {
String patientId;
String nurseId;
String username;
DateTime date;
int age;
List<String> diseases;
//final fotograf olacak
Patients({#required this.patientId,this.nurseId,this.date,this.username,this.age,this.diseases});
factory Patients.fromJson(Map<String, dynamic> json){
return Patients(
patientId: json["patientId"],
nurseId: json["nurseId"],
username: json["username"],
date: json["date"],
age: json["age"],
diseases: json["diseases"],
);
}
Map<String,dynamic> toMap(){
return {
"patientId": patientId,
"nurseId" : nurseId,
"username" : username,
"date" : date,
"age" : age,
"diseases" : diseases,
};
}
}
PatientsProvider dart file;
import 'package:flutter/material.dart';
import 'package:imlearningfirebase/model/patients.dart';
import 'package:uuid/uuid.dart';
import 'package:imlearningfirebase/services/fireStoreService.dart';
class PatientsProvider with ChangeNotifier{
final fireStoreService = FireStoreService();
String _patientId;
String _nurseId;
DateTime _date;
String _username;
int _age;
List<String> _diseases;
var uuid = Uuid();
///Getters
String get username =>_username;
int get age => _age;
List<String> get diseases => _diseases;
DateTime get date => _date;
Stream<List<Patients>> get getPatients => fireStoreService.getEntries();
savePatient(int agee,String usernamee,List<String> diseasess){
if(_nurseId == null){
///add
var newPatient = Patients(patientId: uuid.v1(),nurseId:fireStoreService.getCurrentUserId().toString(),date:DateTime.now(),username: usernamee,age: agee,diseases: diseasess);
print(newPatient.username);
fireStoreService.setPatients(newPatient);
}else{
var updatedPatients = Patients(patientId: uuid.v1(),nurseId:fireStoreService.getCurrentUserId().toString(),date:DateTime.now(),username: usernamee,age: agee,diseases: diseasess);
fireStoreService.setPatients(updatedPatients);
}
}
}
FireStoreService dart file;
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
/// Local Importlar
import '../model/patients.dart';
class FireStoreService{
FirebaseFirestore _db = FirebaseFirestore.instance;
final FirebaseAuth auth = FirebaseAuth.instance;
///Get Entries
Stream<List<Patients>> getEntries(){
return _db
.collection("patients")
.snapshots()
.map((snapshot) => snapshot.docs
.map((doc) => Patients.fromJson(doc.data()))
.toList());
}}
Home screen;
import 'package:flutter/material.dart';
import 'package:imlearningfirebase/provider/patienstProvider.dart';
import 'package:provider/provider.dart';
class HomePage extends StatelessWidget{
#override
Widget build(BuildContext context) {
final patientsProvider = Provider.of<PatientsProvider>(context);
return Scaffold(
appBar: AppBar(
title: Text("Patients"),
),
floatingActionButton: Padding(
padding: const EdgeInsets.fromLTRB(0, 0, 0, 50.0),
child: FloatingActionButton(
onPressed: () async {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => AddPatient()),
);
},
child: Icon(
Icons.add,
color: Colors.blueAccent,
),
backgroundColor: Colors.green,
),
),
body:StreamBuilder<List<Patients>>(
stream: patientsProvider.getPatients,
builder: (context,snapshot){
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (context,index){
return ListTile(
trailing:
Icon(Icons.edit, color: Theme.of(context).accentColor),
title: Text('${DateTime.now()}'
),
);
}
);
}
)
);
}
}
I am trying to create a system where a person has to register and register another person. I created a model so that the registered person creates the profile of the other person. The name of this model is patients. I use the provider package to access patients functions. I have created a function named take patients in Firestore and put it in the patient provider. Finally, I call this on the Home page. but the length came null. I can not see the error I would be glad if you help.
I didn't use your FireStoreService() class. This might be a bit messy but it worked for me. let me know if it works.
Try this:
class PatientsProvider with ChangeNotifier {
...
List<Patients> _patients = [];
// ///Getters
...
// Stream<List<Patients>> get getPatients => fireStoreService.getEntries();
List<Patients> get getPatients => _patients;
FirebaseFirestore _fs = FirebaseFirestore.instance;
StreamSubscription<QuerySnapshot> _stream;
PatientsProvider() {
_stream = _fs.collection('patients').snapshots().listen((snapshot) {
_patients = [];
snapshot.docs.forEach((queryDocumentSnapshot) {
_patients.add(Patients.fromJson(queryDocumentSnapshot.data()));
});
notifyListeners();
});
}
#override
void dispose() {
super.dispose();
_stream.cancel();
}
// savePatient(int agee, String usernamee, List<String> diseasess) {
...
// }
}
Then instead if creating a streambuilder, the provider will update the list when the data changes:
#override
Widget build(BuildContext context) {
PatientsProvider _provider = Provider.of<PatientsProvider >(context);
return Scaffold(
body: ListView.builder(
itemCount: _provider.getPatients.length,
itemBuilder: (BuildContext context, int index) {
return ListTile(
trailing: Icon(Icons.edit, color: Theme.of(context).accentColor),
title: Text('${...}'),
);
},
),
);
}
To get more information on the listener see this documentation:
https://firebase.google.com/docs/firestore/query-data/listen
I am trying to get a list of data from a firebase database and display it as a list in my flutter app, at the moment I cannot get the data to appear, on the UI.When I try and print the list it does not contain any values when it should show the names I have entered in the database.
This is the database that I am trying to retrieve:
This is the code that I have implemented for my UI, any ideas on what is the issue here?
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:firebase_database/firebase_database.dart';
import 'package:firebase_database/ui/firebase_animated_list.dart';
import 'SignInSuccessPage.dart';
class SignInPage extends StatefulWidget {
#override
SignInPageState createState() => SignInPageState();
}
class SignInPageState extends State<SignInPage> {
List<Volunteer> volunteers;
Volunteer volunteer;
DatabaseReference volunteerRef;
#override
void initState() {
super.initState();
volunteers = new List();
volunteer = Volunteer("","", "");
final FirebaseDatabase database = FirebaseDatabase.instance; /
volunteerRef = database.reference().child('volunteerapp-cec4f');
volunteerRef.onChildAdded.listen(_onEntryAdded);
volunteerRef.onChildChanged.listen(_onEntryChanged);
}
_onEntryAdded(Event event) {
setState(() {
volunteers.add(Volunteer.fromSnapshot(event.snapshot));
});
}
_onEntryChanged(Event event) {
var old = volunteers.singleWhere((entry) {
return entry.key == event.snapshot.key;
});
setState(() {
volunteers[volunteers.indexOf(old)] = Volunteer.fromSnapshot(event.snapshot);
});
}
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('List of Names'),
),
resizeToAvoidBottomPadding: false,
body: Column(
children: <Widget>[
Flexible(
child: FirebaseAnimatedList(
query: volunteerRef,
itemBuilder: (BuildContext context, DataSnapshot snapshot,
Animation<double> animation, int index) {
return new ListTile(
title: Text(volunteers[index].firstName),
subtitle: Text(volunteers[index].lastName),
);
},
),
),
],
),
);
}
}
//the volunteer class which contains all the info about each volunteer object and links to firebase
class Volunteer {
String key;
String firstName;
String lastName;
Volunteer(this.key, this.firstName, this.lastName);
Volunteer.fromSnapshot(DataSnapshot snapshot)
: key = snapshot.key,
firstName = snapshot.value["firstName"],
lastName = snapshot.value["lastName"];
toJson() {
return {
"key": key,
"firstName": firstName,
"lastName": lastName,
};
}
}
You don't need to include the name of your project as a child call, it's already in the base reference.
So:
volunteerRef = database.reference();
i'm new to flutter
i try to do an http call and then populate a list. The problem is that the http call works fine but the UI doesn't refresh.
i think that the problem is that the framework create ui with the list before the http call is finish. Than when the http call is finished i'm unable to update the ui.
i've search but i didn't find anything.
EDIT--FOUND SOLUTION IN Listview.Builder code here
http call
static Future<Map> getData() async {
try{
http.Response res = await http.get("http://....");
Map data = JSON.decode(res.body);
print(res.body);
return data;
}
catch(exception){
//todo
}
}
main
class _MyHomePageState extends State<MyHomePage> {
List<Widget> _items = new List<GithubCardItem>();
#override
void initState() {
super.initState();
print("start download");
_downloadData();
}
#override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(title: new Text(widget.title)),
body: new ListView.builder(
itemBuilder: (BuildContext context, int index) => _items[index],
itemExtent: 128.0,
itemCount: _items.length,
),
floatingActionButton: new FloatingActionButton(
child: new Icon(Icons.add),
onPressed: (){
_downloadData();
},
)
);
}
GithubCard _extractDataFromJson(Map githubCard){
GithubCard card = new GithubCard(githubCard["name"], githubCard["avatar_url"], githubCard["description"], githubCard["stargazers_count"], githubCard["open_issued_count"]);
return card;
}
void _downloadData(){
MyHttpCall.getData().then((jsonData) {
//check if i have card to display
if(jsonData["items"] != null){
for(var githubCard in jsonData["items"]){
setState(() {
GithubCard card = _extractDataFromJson(githubCard);
this._items.add(new GithubCardItem(card));
});
print("adding elements");
}
}
});
}
}
the GithubCardItem is a simple stateless widget that return a text.
There are different ways to use ListView as well as get data from network
here is the sample code for your problem
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
List list = new List();
void fetchData() {
getData().then((res) {
setState(() {
list.addAll(res);
});
});
}
#override
void initState() {
super.initState();
fetchData();
}
#override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
),
body: new Center(
child: new ListView.builder(
itemCount: list.length,
itemBuilder: ((BuildContext _context, int position) {
return new ListTile(
title: new Text( list[position]['login'].toString()),
subtitle: new Text(list[position]['url']),
leading: new Image.network(list[position]['avatar_url']),
);
}),
),
),
floatingActionButton: new FloatingActionButton(
onPressed: fetchData,
tooltip: 'Increment',
child: new Icon(Icons.add),
),
);
}
Future<List> getData() async {
var url = "https://api.github.com/users";
List data = new List();
var httpClient = new HttpClient();
var request = await httpClient.getUrl(Uri.parse(url));
var response = await request.close();
if (response.statusCode == HttpStatus.OK) {
var jsonString = await response.transform(utf8.decoder).join();
data = json.decode(jsonString);
return data;
}else{
return data;
}
}
}
Try this:
import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {----------- }
class MyHomePage extends StatefulWidget {-----------}
class _MyHomePageState extends State<MyHomePage> {
List data;
Future<String> getData() async {
var response = await http.get(
Uri.encodeFull("https://api.github.com/users"),
headers: {"Accept": "application/json"});
this.setState(() {
data = json.decode(response.body);
});
return "Success!";
}
#override
void initState() {
this.getData();
}
#override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("Listviews"),
),
body: new ListView.builder(
itemCount: data == null ? 0 : data.length,
itemBuilder: (BuildContext context, int position) {
return new ListTile(
title: new Text(data[position]['login'].toString()),
subtitle: new Text(data[position]['url']),
leading: new Image.network(data[position]['avatar_url']),
);
},
),
);
}
}
Made bit more simple and it's working. Check the sample here