I see several questions and answers about Flutter for mobile that use stream builder like this:
body: new StreamBuilder(
stream: Firestore.instance.collection("collection").snapshots(),
builder: (context, snapshot) {
...
I'm trying to do the same on flutter for the web, but in my configuration, the snapshots() method is unknown, generating an exception while running (and a vscode warning beforehand). Why? Do I have an incorrect setup?
I've followed these steps which I found here and elsewhere:
1) Included firebase as a dependency in pubspec.yaml
dependencies:
flutter:
sdk: flutter
firebase: ^6.0.0
2) Included the firestore js scripts in the index.html body tag:
<script src="https://www.gstatic.com/firebasejs/7.5.0/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.5.0/firebase-analytics.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.5.0/firebase-firestore.js"></script>
<script src="main.dart.js" type="application/javascript"></script>
3) In main.dart, imported firebase.dart files (using advice given here, though I'm not exactly sure which step above got me access to this package. I'm a flutter nube, if it isn't obvious)
import 'package:flutter/material.dart';
import 'package:firebase/firebase.dart' as fb;
import 'package:firebase/firestore.dart' as fs;
Having followed these steps, I can get this code working....
void main() {
if (fb.apps.length == 0) {
try {
fb.initializeApp(
apiKey: "mike",
authDomain: "myauthdomain",
databaseURL: "mydburl",
projectId: "myproductid",
storageBucket: "mystoragebucket",
);
} catch(e) {
print(e);
}
}
fs.Firestore store = fb.firestore();
fs.CollectionReference ref = store.collection("MyCollection");
ref.onSnapshot.listen((querySnapshot) {
querySnapshot.docs.forEach((doc) {
print(doc.data()); // this works!!
});
});
runApp(MyApp());
}
But, as I mentioned earlier, getting the stream builder working, all of the advice suggests that I can get a stream of snapshots by saying...
class MyList extends StatelessWidget {
#override
Widget build(BuildContext context) {
return new StreamBuilder(
stream: fb.firestore().collection('MyCollection').snapshots(),
...
The packages I have running on web don't seem to have anything like the snapshots method (or property) on a firestore collection reference. Can somebody set me straight?
I had the same issue when I tried to create a web version of my flutter mobile app which uses a lot of StreamBuilders. The following works for me:
Pubspec.yaml dependencies:
firebase_web: ^5.0.9
firebase_core: ^0.4.3+2
In your code:
import 'package:firebase_web/firestore.dart';
import 'package:firebase_web/firebase.dart';
body: new StreamBuilder(
stream: firestore().collection('collection').onSnapshot,
builder: (context, snapshot) {
...
Below I have included a list with changes that I encountered so far going from a flutter mobile app to a flutter web app:
documents = docs as in snapshot.data.docs.length
documents() = doc() as in firestore().collection('foo').doc('bar')
reference = ref as in snapshot.ref
data = data() as in snapshot.data()
The method setData from Firestore = set
The method updateData from Firestore = update(data:{'yourKey': 'yourValue',})
Hope this can help!
The querySnapshot.docs property returns a List<DocumentSnapshot>, while you need a stream for the stream property where each item on the stream is a list.
I've only needed this with the FlutterFire libraries for iOS/Android, but it should look something like this:
Stream<List<DocumentSnapshot>> getStream() async* {
fb.firestore().collection("MyCollection").onSnapshot.listen((querySnapshot) {
yield querySnapshot.docs;
}
}
Related
I recently went back to a Flutter project I've not touched for a couple of months and updated a few bits and pieces. Now, when I run the app in my emulator, I get the message:
[core/no-app] No Firebase App '[DEFAULT]' has been created
I added this line of code to initialize it in the app: await Firebase.initializeApp();... but it now doesn't appear to connect to my Firebase, just an empty one that I presume it has created on the fly.
This was my original code, which according to this site is the correct way to initialize the app: https://firebase.flutter.dev/docs/overview/
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
final Future<FirebaseApp> _initialization = Firebase.initializeApp();
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark(),
home: FutureBuilder(
future: _initialization,
builder: (context, snapshot) {...
Any idea what I'm doing wrong? The original app on my phone works fine with Firebase, so I presume it's something to do with updating Flutter, Firebase to the latest versions.
Thanks
You should initalize your app in the main function before you run your app, not in the MyApp widget.
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(); // add this line
runApp(MyApp());
}
Ah, it wasn't anything to do with the incorrect Firebase app. I did initialize it in the main function, but in debugging I found that it was hitting an error in reading an object from the database. It was just incorrectly defined. I fixed that and it works now.
Thanks
I downloaded flutter chat app on Github and I was learning through it. The original developer used Firestore but in mine, I am getting an error "Undefined name 'Firestore'.
Try correcting the name to one that is defined, or defining the name" like this.
I search for this and I read in cloudfirestore docs we can use "FirebaseFirestore" (maybe I am wrong). I am learning to write backend with flutter and so far I did UI parts. so this is my first attempt at learning backend with flutter.
handleSignIn() async {
final res = await googleSignIn.signIn();
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
final auth = await res.authentication;
final credentials = GoogleAuthProvider.getCredential(
idToken: auth.idToken, accessToken: auth.accessToken);
final firebaseUser =
(await firebaseAuth.signInWithCredential(credentials)).user;
if (firebaseUser != null) {
final result = (await Firestore.instance
.collection('users')
.where('id', isEqualTo: firebaseUser.uid)
.getDocuments())
.documents;
if (result.length == 0) {
///new user
Firestore.instance
.collection('users')
.document(firebaseUser.uid)
.setData({
"id": firebaseUser.uid,
"name": firebaseUser.displayName,
"profile_pic": firebaseUser.photoURL,
"created_at": DateTime.now().millisecondsSinceEpoch,
});
sharedPreferences.setString("id", firebaseUser.uid);
sharedPreferences.setString("name", firebaseUser.displayName);
sharedPreferences.setString("profile_pic", firebaseUser.photoURL);
Navigator.of(context)
.push(MaterialPageRoute(builder: (context) => Home()));
} else {
///Old user
sharedPreferences.setString("id", result[0]["id"]);
sharedPreferences.setString("name", result[0]["name"]);
sharedPreferences.setString("profile_pic", result[0]["profile_pic"]);
Navigator.of(context)
.push(MaterialPageRoute(builder: (context) => Home()));
}
}
}
this is the error I'm getting
so can you guys explain how to solve this error?
Thank You.
I think you are missing to add cloud_firestore dependency in pubspec.yml. One more thing,
you are using latest version, use "FirebaseFirestore" instead of "Firestore".
Go to pubspec.yaml file in your flutter project.
Add this( cloud_firestore: ^0.16.0 ) below dependencies like in the image below.
You can skip this step if you use VScode. It does this automatically if you save the file after the update.
IF NOT , do flutter pub get inside your project. (this fetches the new packages added to the project, in our case cloud_firestore)
Make sure that you are able to see this import in the file you are getting the errors in.
import 'package:cloud_firestore/cloud_firestore.dart';
Next, update Firestore.instance -> FirebaseFirestore.instance in all your files where you are getting this error.
Use this code --
FirebaseFirestore.instance
.collection("users").add({"name": "Majeed"});
change Firestore with FirebaseFirestore and remove the Document and setData
replace setData with add.
While Firebase.initializeApp() only needs to be called once, are there negative consequences for calling it twice?
Background: I'm troubleshooting a [core/no-app] No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp() error and temporarily fixed it by adding await Firebase.initializeApp(); in void main() async in addition to my pre-existing final Future<FirebaseApp> _fbApp = Firebase.initializeApp();
Everything seems to work okay now. I intend to fix it, but if calling Firebase.initializeApp() twice isn't hurting anything, I can stick with my immediate priorities and move forward.
Here's the relevant block of code:
void main() async {
WidgetsFlutterBinding
.ensureInitialized(); // added per https://stackoverflow.com/questions/57689492/flutter-unhandled-exception-servicesbinding-defaultbinarymessenger-was-accesse
await Firebase
.initializeApp();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
final Future<FirebaseApp> _fbApp = Firebase
.initializeApp(); // changed from "async { await Firebase.initializeApp();" per official "Getting started with Firebase on Flutter - Firecasts" video at https://youtu.be/EXp0gq9kGxI?t=920
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return StreamProvider<Userrr>.value(
value: AuthService().user,
// above specifies what stream we want to listen to and what data we expect to get back
child: MaterialApp(
Thanks!
UPDATE: I tried all the good advice below and nothing seemed to work. I think my code, an exact duplication from two tutorials (one for getting started with Firebase and another for Firebase auth 101) had one or more gaps because of package updates or other incompatibilities.
I went back to basics and wrote-out by hand and re-implemented every step for installing and setting-up Firebase Core from the official "FlutterFire Overview."
I re-read all the documentation, as suggested below.
I updated all packages, including firebase_auth: “^0.20.0" to firebase_auth: “^0.20.0+1" (the +1 change is to “FIX: package compatibility,” per the changelog).
And then finally, I created a backup of main.dart as old_main.dart, then copy-pasted the exact "Initializing FlutterFire" FurtureBuilder code, then carefully replaced each part of that generic code with my own. Here is each item I replaced:
// replaced "_initialization" with "_fbApp"
// replaced if (snapshot.hasError) ... "return SomethingWentWrong();" with the response from below
// replaced "return Loading();" with CircularProgressIndicator form below
// changed return MyAwesomeApp(); to return MyApp();
// changed "class App extends StatelessWidget" to "class MyApp extends StatelessWidget
// replaced "MyAwesomeApp();" from "if (snapshot.connectionState == ConnectionState.done) { return MyAwesomeApp();" with all the "StreamProvider<Userrr>.value(..." code EXCEPT changed home to "home: Wrapper(),"
It may seem elementary, but for a novice like myself, it was the only way forward. Thankfully it worked!
Here's the full working code excerpt:
void main() {
WidgetsFlutterBinding
.ensureInitialized(); // added by mgav, per https://stackoverflow.com/questions/57689492/flutter-unhandled-exception-servicesbinding-defaultbinarymessenger-was-accesse
// await Firebase.initializeApp(); // added by mgav to test, KNOWING the Firebase is already initialized as a future below in line 25. Was trying to test (temp?) fix for error: “[core/no-app] No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp() The relevant error-causing widget was: MyApp file:///Users/mgav/AndroidStudioProjects/brand_new_flutter_app/lib/main.dart:21:10”
runApp(MyApp());
}
// BEGIN Firebase FutureBuilder code pasted from docs at https://firebase.flutter.dev/docs/overview/#initializing-flutterfire (replaces next section of commented-out code)
class MyApp extends StatelessWidget {
// Create the initialization Future outside of `build`:
final Future<FirebaseApp> _fbApp = Firebase.initializeApp();
#override
Widget build(BuildContext context) {
return FutureBuilder(
// Initialize FlutterFire:
future: _fbApp,
builder: (context, snapshot) {
// Check for errors
if (snapshot.hasError) {
print('You have an error! ${snapshot.error.toString()}');
return Text('Something went wrong main.dart around line 48');
}
// Once complete, show your application
if (snapshot.connectionState == ConnectionState.done) {
return StreamProvider<Userrr>.value(
value: AuthService().user,
// above specifies what stream we want to listen to and what data we expect to get back
child: MaterialApp(
title: 'Real Revs and Q&A',
theme: ThemeData(
primarySwatch: Colors.blueGrey,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
routes: {
// '/welcome': (context) => WelcomeScreen(),
'/cleanwritereview': (context) => CleanWriteReviewScreen(),
'/registrationscreen': (context) => RegistrationScreen(),
'/loginscreen': (context) => LoginScreen(),
'/viewreviewsscreen': (context) => ViewReviewsScreen(),
'/homescreen': (context) => Home(),
},
home: Wrapper(),
),
);
}
// Otherwise, show something whilst waiting for initialization to complete
return Center(
child: CircularProgressIndicator(),
);
},
);
}
}
You'll get an error message if you call initializeApp() twice for the same FirebaseApp.
In your case, you can get the app that you've already created with:
final FirebaseApp _fbApp = Firebase.app();
Also see the documentation on FlutterFire, specifically initializing and using FirebaseApp.
To initialise firebase you either do:
main(){
await Firebase.initializeApp(); // don't run the app until firebase is initialized
runApp(MyApp());
}
Or use a FutureBuilder which ensure the future is resolved before running the code inside the builder function.
#override
Widget build(BuildContext context) {
final _fbApp = Firebase.initializeApp();
return FutureBuilder(
future: _fbApp,
builder: (context, snapshot) { // waits until _fbApp is resolved to execute.
....
});
}
You get an error because you don't await _fbApp future.
In your code there is no guarantee AuthService().user is executed after initializeApp has finished. To garantee this you have to wait until initializeApp() is resolved by using await, then or a FutureBuilder.
Add a try catch to understand why the first call in initializeApp is not working.
Firebase initialises its core services only once. there is exactly one FirebaseApp instance by name. When you don't specify the name '[DEFAULT]' is used.
Try doing this:
final app = await Firebase.initializeApp();
final app2 = await Firebase.initializeApp();
print(app == app2); // is true
Please provide more details on your setup:
firebase_auth, firebase_core versions,
Execution plateform (Android, ios or web).
In the last version of fire_auth we use:
FirebaseAuth.instance.authStateChanges // stream to subscribe to the user's current authentication state.
I am trying to fetch data from Friebase-firestore and then convert it to model, I used the same code and approach to another project (web project) ,it works fine , but with this project it doesn't work ,I don't know why. I upgrade flutter version, run Flutter doctor , and nothing wrong with the environment setup.
here is the code :
class dbService{
static final CollectionReference myCollection =
FirebaseFirestore.instance.collection('myCollection');
static Future<myModel> getMyInfo() async{
myModel sanaa ;
print('this line to print');
final QuerySnapshot myInfo =await myCollection.limit(1).get();
myInfo.docs.forEach((element) {
print(element.data()['name']);
sanaa= new myModel(
name:element.data()['name'].toString(),
title: element.data()["title"].toString(),
img: element.data()['img'].toString(),
);
print('\n print ------ ');
print(element.data()['name']);
print(' that is');
}
);
return sanaa;
}
}
class dbManager{
Stream<myModel> myInfoStream() async*{
yield await dbService.getMyInfo();
}
}
class _aboutStateState extends State<aboutState> {
Stream stream ;
dbManager mng = new dbManager();
#override
void initState() {
// TODO: implement initState
stream = mng.myInfoStream();
super.initState();
}
#override
Widget build(BuildContext context) {
return Container(
color: Colors.pink[300],
child: StreamBuilder<myModel>(
stream: stream,
builder: (context, snapshot){
if(snapshot.hasError){
print(snapshot.error);
return Center(child: Text('Something went wrong , please try again later'),);
}
else if(snapshot.hasData){
return Text("${snapshot.data.name}");
}
else {
return Center(child: CircularProgressIndicator());
}
},
),
I got this error
this line to print
[cloud_firestore/unknown] NoSuchMethodError: invalid member on null: 'includeMetadataChanges'
but when I connect directly to the firestore without converting it into model ,It works fine and print the data in the screen , the code :
child:FutureBuilder(
future: me.doc(id).get(),
builder: (context, snapshot){
if(snapshot.hasError){
print(snapshot.error);
return Center(child: Text('Something went wrong , please try again later'),);
}
else if(snapshot.hasData){
// print("${snapshot.data.data()['app_pics'][1]} \n${snapshot.data.data()['img']}");
return Text("${snapshot.data.data()['app_pics'][1]}");
}
else {
return Center(child: CircularProgressIndicator());
}
},
),
I run flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel beta, 1.24.0-10.2.pre, on Microsoft Windows [Version 6.3.9600], locale
en-US)
[√] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
[√] Chrome - develop for the web
[√] Android Studio (version 4.0)
[√] VS Code (version 1.51.0)
[√] Connected device (3 available)
• No issues found!
Please I need help
I found the answer, it caused because of the version of Firebase JS SDK .
I replace 8.1.1 to 7.22.1 in index.html file
<!-- The core Firebase JS SDK is always required and must be listed first -->
<!-- <script src="https://www.gstatic.com/firebasejs/8.1.1/firebase-app.js"></script>-->
<!-- <!– TODO: Add SDKs for Firebase products that you want to use-->
<!-- https://firebase.google.com/docs/web/setup#available-libraries –>- ->
<!-- <script src="https://www.gstatic.com/firebasejs/8.1.1/firebase-firestore.js">
</script>-->
<!-- <script src="https://www.gstatic.com/firebasejs/8.1.1/firebase-analytics.js"></script>-->
I am using 7.22.1 version for Firebase JS SDK
<script src="https://www.gstatic.com/firebasejs/7.22.1/firebase-app.js"></script>
<!-- TODO: Add SDKs for Firebase products that you want to use
https://firebase.google.com/docs/web/setup#available-libraries -->
<script src="https://www.gstatic.com/firebasejs/7.22.1/firebase-firestore.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.22.1/firebase-analytics.js"></script>
You can also use 8.0.1 as mentioned here and subscribe to the issue. Then you get (hopefully) notified once it´s fixed.
https://github.com/FirebaseExtended/flutterfire/issues/4127#issuecomment-754171740
Try out this versions:
firebase_core: ^0.5.2
cloud_firestore: ^0.14.3
For more info check out this github Link
I try to read listen to a stream from Firebase with this code:
visibleListsIds.forEach((final String listId) async {
final Stream<List<WishList>> wishListStream = sharedCollection()
.document(listId)
.snapshots()
.map((DocumentSnapshot documentSnapshot) {
log.d("updated Document Snapshot: ${documentSnapshot.data}");
return [
_getSerializers()
.deserializeWith(WishList.serializer, documentSnapshot.data)
];
});
wishListStreams.add(wishListStream);
});
Where sharedCollection() gives me access to the Firestore instance with the correct collection
I try to write to the collection, with this code
DocumentReference postRef = sharedCollection().document(wishList.listId);
firestore.runTransaction((Transaction tx) async {
DocumentSnapshot postSnapshot = await tx.get(postRef);
if (postSnapshot.exists) {
await tx.update(postRef,
_getSerializers().serializeWith(WishList.serializer, wishList));
} else {
await tx.set(postRef,
_getSerializers().serializeWith(WishList.serializer, wishList));
}
});
What happens:
I can write to Firebase but only one change at a time. When I do the next update, the last one gets reverted.
I can see the updated data only in the Firebase Console. The App does not show it and it does not show in the log at log.d("updated Document Snapshot: ${documentSnapshot.data}");.
When I modify data in the Firebase Console, I can also not see it change
BUT once I reload the App, all the Data syncs up to the current state of the Firebase Console
Anyone know why I do not get updates with the Stream?
Thanks for your help.
I use the Cloud Firestore Plugin:
cloud_firestore: ^0.13.0+1