Skip to content

Instantly share code, notes, and snippets.

@edutrul
Created June 6, 2018 05:27
Show Gist options
  • Save edutrul/a4eeb0fd00078d6ce0bc3f11acfb381e to your computer and use it in GitHub Desktop.
Save edutrul/a4eeb0fd00078d6ce0bc3f11acfb381e to your computer and use it in GitHub Desktop.
babies_main_dart_correct_google.dart (this is correct version rather than in https://codelabs.developers.google.com/codelabs/flutter-firebase/#8 )
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
const MyApp();
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Baby Names',
home: const MyHomePage(title: 'Baby Name Votes'),
);
}
}
class MyHomePage extends StatelessWidget {
const MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(title: new Text(title)),
body: new StreamBuilder<QuerySnapshot>(
stream: Firestore.instance.collection('baby').snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (!snapshot.hasData) return new Text('Loading...');
return new ListView(
children: snapshot.data.documents.map((DocumentSnapshot document) {
return new ListTile(
title: new Text(document['name']),
subtitle: new Text('${document['votes']} votes'),
);
}).toList(),
);
},
)
);
}
}
@chintooflutter
Copy link

chintooflutter commented Feb 17, 2019

I am try above simple code to get data from firebase but I am getting error for "timestampsInSnapshotsEnabled."

W/Firestore(23432):
W/Firestore(23432): FirebaseFirestore firestore = FirebaseFirestore.getInstance();
W/Firestore(23432): FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder()
W/Firestore(23432): .setTimestampsInSnapshotsEnabled(true)
W/Firestore(23432): .build();
W/Firestore(23432): firestore.setFirestoreSettings(settings);
W/Firestore(23432):
W/Firestore(23432): With this change, timestamps stored in Cloud Firestore will be read back as com.google.firebase.Timestamp objects instead of as system java.util.Date objects. So you will also need to update code expecting a java.util.Date to instead expect a Timestamp.

=========================================

So i entered below code with "timestampsInSnapshotsEnabled: true" but still i am getting same error.
Please, can you help me how to resolve the error?
Use Latest Version of Cloudfirestore pubspec.yaml file

cloud_firestore:

============================================
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:cloud_firestore/cloud_firestore.dart';

Future main() async {
final FirebaseApp app = await FirebaseApp.configure(
name: 'vscodefirebase',
options: const FirebaseOptions(
googleAppID: 'xxxx',
gcmSenderID: 'xxx',
apiKey: 'xxx',
projectID: 'xxxx',
),
);
final Firestore firestore = Firestore(app: app);
await firestore.settings(timestampsInSnapshotsEnabled: true);

runApp(MyApp());
}

class MyApp extends StatelessWidget {
const MyApp();

@OverRide
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Baby Names',
home: const MyHomePage(title: 'Baby Name Votes'),
);
}
}

class MyHomePage extends StatelessWidget {
const MyHomePage({Key key, this.title}) : super(key: key);

final String title;

@OverRide
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(title: new Text(title)),
body: new StreamBuilder(
stream: Firestore.instance.collection('baby').snapshots(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (!snapshot.hasData) return new Text('Loading...');
return new ListView(
children: snapshot.data.documents.map((DocumentSnapshot document) {
return new ListTile(
title: new Text(document['name']),
subtitle: new Text('${document['votes']} votes'),
);
}).toList(),
);
},
)
);
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment