Reputation: 75
I'm developing my first flutter APP, however i'm really newbie such on firebase and in flutter.
my app is already authenticating on firebase through email and password, but I only know how to display the db collection to all the users,I would like to display the specific data of the logged user.
for now Im getting this error
Class 'DocumentSnapshot' has no instance getter 'documents'. Receiver: Instance of 'DocumentSnapshot' Tried calling: documents
but when i call documents its shows a new error: The method 'documents' isn't defined for the type 'CollectionReference'. Try correcting the name to the name of an existing method, or defining a method named 'documents'.
MY signIn CODE
signIn() async {
if (_formKey.currentState.validate()) {
_formKey.currentState.save();
try {
getData() async{
String user = (await FirebaseAuth.instance.currentUser).uid;
return Firestore.instance.collection('users').document(user);
}
UserCredential user = await FirebaseAuth.instance
.signInWithEmailAndPassword(email: _email, password: _password);
getUserDoc();
Navigator.push(
context, MaterialPageRoute(builder: (context) => Home()));
}
catch (e) {
print(e.message);
}
}
}
}
home Page CODE
import 'dart:typed_data';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:dio/dio.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:esys_flutter_share/esys_flutter_share.dart';
import 'dart:io';
import 'package:http/http.dart';
import 'package:flutter_cached_pdfview/flutter_cached_pdfview.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:flutter_downloader/flutter_downloader.dart';
import 'package:path_provider/path_provider.dart';
import 'package:image_viewer/image_viewer.dart';
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
int _currentIndex = 0;
Stream<DocumentSnapshot> getDatabase() async* {
FirebaseUser user = await FirebaseAuth.instance.currentUser;
yield* Firestore.instance
.collection('users')
.document(user.uid)
.snapshots();
}
final tabs = [
//TAB DE MANUAIS
Center(
child: (Scaffold(
body: StreamBuilder (
stream: FirebaseFirestore.instance.collection('users').document('user.uid').snapshots(),
builder: (context, snapshot) {
if (snapshot.data == null) return CircularProgressIndicator();
return Container(
padding: EdgeInsets.all(16),
child: ListView.builder(
itemCount: snapshot.data.documents.length,
itemBuilder: (context, index) {
DocumentSnapshot manuais =
snapshot.data.documents[index];
var dio = Dio();
return Card(
color: Colors.grey[250],
child: Container(
padding: EdgeInsets.all(10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
new Image.asset('Images/pdflogo.png', width: 32,
),
Center(
child: Text(
(manuais.data()['nome'].toString()),
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(fontSize: 16),
),
),
ButtonBar(
children: <Widget>[
FlatButton(
child: const Text('Compartilhar / Download'),
onPressed: () async {
var request = await HttpClient().getUrl(Uri.parse(manuais.data()['documento']));
var response = await request.close();Uint8List bytes = await consolidateHttpClientResponseBytes(response);
await Share.file(
'ESYS AMLOG',
'Manual.pdf',
bytes,
'image/jpg');
}),
],
),
],
),
),
);
}),
);
})))),
//TAB DE PRODUCAO
Center(
child: (Scaffold(
body: StreamBuilder(
stream: FirebaseFirestore.instance.collection('producao').snapshots(),
builder: (context, snapshot) {
if (snapshot.data == null) return CircularProgressIndicator();
return Container(
padding: EdgeInsets.all(16),
child: ListView.builder(
itemCount: snapshot.data.documents.length,
itemBuilder: (context, index) {
DocumentSnapshot producao =
snapshot.data.documents[index];
return Card(
color: Colors.grey[250],
child: Container(
padding: EdgeInsets.all(10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Center(
child: Image.network(producao.data()['img'].toString(), width: 260,
),
),
Text(
(producao.data()['data'].toString()),
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(fontSize: 22),
),
Text(
(producao.data()['detail'].toString()),
style: TextStyle(fontSize: 16),
),
ButtonBar(
children: <Widget>[
FlatButton(
child: const Text('DETALHES'),
onPressed: () {
ImageViewer.showImageSlider(
images: [
(producao.data()['img']),
//List of images' URLs
],
);
}),
FlatButton(
child: const Text('COMPARTILHAR'),
onPressed: () async {
var request = await HttpClient().getUrl(Uri.parse(producao.data()['img']));
var response = await request.close();Uint8List bytes = await consolidateHttpClientResponseBytes(response);
await Share.file(
'ESYS AMLOG',
'amlog.jpg',
bytes,
'image/jpg'
);
}),
],
),
],
),
),
);
}),
);
})))),
Center(child: Text('Documentos')),
];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Área do Cliente')),
body: tabs[_currentIndex],
bottomNavigationBar: BottomNavigationBar(
currentIndex: _currentIndex,
type: BottomNavigationBarType.shifting,
iconSize: 28,
items: [
BottomNavigationBarItem(
icon: Icon(Icons.picture_as_pdf),
title: Text('Manuais'),
backgroundColor: Colors.indigo),
BottomNavigationBarItem(
icon: Icon(Icons.build),
title: Text('Produção'),
backgroundColor: Colors.indigo),
BottomNavigationBarItem(
icon: Icon(Icons.folder),
title: Text('Documentos'),
backgroundColor: Colors.indigo,
)
],
onTap: (index) {
setState(() {
_currentIndex = index;
});
},
),
);
}
}
```
Upvotes: 0
Views: 256
Reputation: 21681
This is the sample that you can get the child value :
FirebaseFirestore.instance.collection('users').document('user.uid').then((DataSnapshot snapshot) {
if(snapshot.value != null){
setState(() {
String firebaseText=snapshot.value['your key name'].toString();
});
}
Upvotes: 2
Reputation: 73
FirebaseFirestore.instance.collection('users').document('user.uid').snapshots()
This particular line is returning just 1 document. It does not give you a list of documents. Now a document will have several fields - Key:Value pairs. It can also have a collection.
To access the fields by keys, use snapshot.data[key]
. It will return the corresponding value
Upvotes: 1