Estoy conectando mi app en Flutter con Firebase, pero estoy teniendo problemas a la hora de leer los datos de Firebase. Esto es lo que tengo escrito:
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
class Home extends StatelessWidget {
final FirebaseUser user;
const Home({
Key key,
this.user
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Home ${user.email}"),
),
body: StreamBuilder<DocumentSnapshot>(
stream: Firestore.instance.collection('users').document(user.uid).snapshots(),
builder: (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot){
if(snapshot.hasError){
return Text("Error: $snapshot.error");
}
switch(snapshot.connectionState){
case ConnectionState.waiting:
return Text("Loading");
default:
return checkRole(snapshot.data);
}
},
),
);
}
Center checkRole(DocumentSnapshot snapshot){
if(snapshot.data['role'] == 'admin'){
return adminPage(snapshot);
} else{
return userPage(snapshot);
}
}
Center adminPage(DocumentSnapshot snapshot){
return Center(child: Text(snapshot.data['role'] + ' PAGE'));
}
Center userPage(DocumentSnapshot snapshot){
return Center(child: Text(snapshot.data['role']));
}
}
El problema es q se queda cargando (en la parte del switch) y no avanza. Probe a eliminar el switch y poner un else {return checkRole(snapshot.data)}, para eliminar la parte de loading, pero sigue sin funcionar.
Este es el error:
E/flutter ( 4074): [ERROR:flutter/shell/common/shell.cc(209)] Dart Error: Unhandled exception:
E/flutter ( 4074): MissingPluginException(No implementation found for method DocumentReference#addSnapshotListener on channel plugins.flutter.io/cloud_firestore)
E/flutter ( 4074): #0 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:159:7)
E/flutter ( 4074): <asynchronous suspension>
E/flutter ( 4074): #1 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:334:12)
E/flutter ( 4074): #2 MethodChannelDocumentReference.snapshots.<anonymous closure> (package:cloud_firestore_platform_interface/src/method_channel/method_channel_document_reference.dart:93:50)
E/flutter ( 4074): #3 _runGuarded (dart:async/stream_controller.dart:823:24)
E/flutter ( 4074): #4 _BroadcastStreamController._subscribe (dart:async/broadcast_stream_controller.dart:213:7)
E/flutter ( 4074): #5 _ControllerStream._createSubscription (dart:async/stream_controller.dart:836:19)
E/flutter ( 4074): #6 _StreamImpl.listen (dart:async/stream_impl.dart:493:9)
E/flutter ( 4074): #7 new _ForwardingStreamSubscription (dart:async/stream_pipe.dart:118:10)
E/flutter ( 4074): #8 _ForwardingStream._createSubscription (dart:async/stream_pipe.dart:88:16)
E/flutter ( 4074): #9 _ForwardingStream.listen (dart:async/stream_pipe.dart:83:12)
E/flutter ( 4074): #10 _StreamBuilderBaseState._subscribe (package:flutter/src/widgets/async.dart:139:37)
E/flutter ( 4074): #11 _StreamBuilderBaseState.didUpdateWidget (package:flutter/src/widgets/async.dart:124:7)
E/flutter ( 4074): #12 StatefulElement.update (package:flutter/src/widgets/framework.dart:4734:58)
E/flutter ( 4074): #13 Element.updateChild (package:flutter/src/widgets/framework.dart:3245:15)
E/flutter ( 4074): #14 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4571:16)
E/flutter ( 4074): #15 Element.rebuild (package:flutter/src/widgets/framework.dart:4262:5)
E/flutter ( 4074): #16 StatelessElement.update (package:flutter/src/widgets/framework.dart:4627:5)
E/flutter ( 4074): #17 Element.updateChild (package:flutter/src/widgets/framework.dart:3245:15)
E/flutter ( 4074): #18 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4571:16)
E/flutter ( 4074): #19 Element.rebuild (package:flutter/src/widgets/framework.dart:4262:5)
E/flutter ( 4074): #20 ProxyElement.update (package:flutter/src/widgets/framework.dart:4906:5)
E/flutter ( 4074): #21 Element.updateChild (package:flutter/src/widgets/framework.dart:3245:15)
E/flutter ( 4074): #22 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4571:16)
E/flutter ( 4074): #23 Element.rebuild (package:flutter/src/widgets/framework.dart:4262:5)
E/flutter ( 4074): #24 ProxyElement.update (package:flutter/src/widgets/framework.dart:4906:5)
E/flutter ( 4074): #25 Element.updateChild (package:flutter/src/widgets/framework.dart:3245:15)
E/flutter ( 4074): #26 RenderObjectElement.updateChildren (package:flutter/src/widgets/framework.dart:5566:32)
E/flutter ( 4074): #27 MultiChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6001:17)
E/flutter ( 4074): #28 Element.updateChild (package:flutter/src/widgets/framework.dart:3245:15)
E/flutter ( 4074): #29 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4571:16)
E/flutter ( 4074): #30 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4719:11)
E/flutter ( 4074): #31 Element.rebuild (package:flutter/src/widgets/framework.dart:4262:5)
E/flutter ( 4074): #32 StatefulElement.update (package:flutter/src/widgets/framework.dart:4751:5)
E/flutter ( 4074): #33 Element.updateChild (package:flutter/src/widgets/framework.dart:3245:15)
E/flutter ( 4074): #34 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4571:16)
E/flutter ( 4074): #35 Element.rebuild (package:flutter/src/widgets/framework.dart:4262:5)
E/flutter ( 4074): #36 ProxyElement.update (package:flutter/src/widgets/framework.dart:4906:5)
E/flutter ( 4074): #37 Element.updateChild (package:flutter/src/widgets/framework.dart:3245:15)
E/flutter ( 4074): #38 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4571:16)
E/flutter ( 4074): #39 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4719:11)
E/flutter ( 4074): #40 Element.rebuild (package:flutter/src/widgets/framework.dart:4262:5)
E/flutter ( 4074): #41 StatefulElement.update (package:flutter/src/widgets/framework.dart:4751:5)
E/flutter ( 4074): #42 Element.updateChild (package:flutter/src/widgets/framework.dart:3245:15)
E/flutter ( 4074): #43 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5881:14)
E/flutter ( 4074): #44
Por si no quedo claro, simplemente quiero mostrar el texto de la base de datos en la pagina. Ejecute flutter clean, flutter doctor, flutter upgrade y flutter pub get, y sigue sin funcionar. Tambien elimine el emulador y lo volvi a instalar, al igual que con la app.
Este seria el pubspect.yaml:
name: flutterfirebase
description: A new Flutter application.
publish_to: 'none'
version: 1.0.0+1
environment:
sdk: ">=2.7.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
firebase_auth: ^0.16.1
cloud_firestore: ^0.13.6
cupertino_icons: ^0.1.3
dev_dependencies:
flutter_test:
sdk: flutter
flutter:
uses-material-design: true
Archivo android/build.gradle:
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.0'
classpath 'com.google.gms:google-services:4.3.3'
}
}
allprojects {
repositories {
google()
jcenter()
}
}
rootProject.buildDir = '../build'
subprojects {
project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
project.evaluationDependsOn(':app')
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Y finalmente android/app/build.gradle:
def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
localPropertiesFile.withReader('UTF-8') { reader ->
localProperties.load(reader)
}
}
def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}
def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
flutterVersionCode = '1'
}
def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
flutterVersionName = '1.0'
}
apply plugin: 'com.android.application'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
android {
compileSdkVersion 28
lintOptions {
disable 'InvalidPackage'
}
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.example.flutterfirebase"
minSdkVersion 16
targetSdkVersion 28
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}
buildTypes {
release {
// TODO: Add your own signing config for the release build.
// Signing with the debug keys for now, so `flutter run --release` works.
signingConfig signingConfigs.debug
}
}
}
flutter {
source '../..'
}
apply plugin: 'com.google.gms.google-services'
La version de flutter es la ultima (Flutter 1.20.0-1.0.pre.42) aunque prove con la version anterior y tampoco funcionaba. Dart esta en la version 2.9.0. Y finalmente uso Android Studio.