Last active
December 11, 2022 16:33
-
-
Save branflake2267/ea80ce71179c41fdd8bbdb796ca889f4 to your computer and use it in GitHub Desktop.
Flutter - Getting data with firebase. I show two ways to get dat in the initState, 1. get data once and 2. and get updates in a stream.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
import 'package:firebase_database/firebase_database.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:shared_preferences/shared_preferences.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.green, | |
), | |
home: new HomePage(), | |
); | |
} | |
} | |
class HomePage extends StatefulWidget { | |
@override | |
_HomePageState createState() => new _HomePageState(); | |
} | |
class _HomePageState extends State<HomePage> { | |
StreamSubscription _subscriptionTodo; | |
String _todoName = "Display the todo name here"; | |
@override | |
void initState() { | |
//FirebaseTodos.getTodo("-KriJ8Sg4lWIoNswKWc4").then(_updateTodo); | |
FirebaseTodos.getTodoStream("-KriJ8Sg4lWIoNswKWc4", _updateTodo) | |
.then((StreamSubscription s) => _subscriptionTodo = s); | |
super.initState(); | |
} | |
@override | |
void dispose() { | |
if (_subscriptionTodo != null) { | |
_subscriptionTodo.cancel(); | |
} | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
var itemTile = new ListTile( | |
title: new Text("$_todoName"), | |
); | |
return new Scaffold( | |
appBar: new AppBar( | |
title: new Text("App Bar Title"), | |
), | |
body: new ListView( | |
children: <Widget>[ | |
itemTile, | |
], | |
), | |
); | |
} | |
_updateTodo(Todo value) { | |
var name = value.name; | |
setState((){ | |
_todoName = name; | |
}); | |
} | |
} | |
class Todo { | |
final String key; | |
String name; | |
Todo.fromJson(this.key, Map data) { | |
name = data['name']; | |
if (name == null) { | |
name = ''; | |
} | |
} | |
} | |
class FirebaseTodos { | |
/// FirebaseTodos.getTodoStream("-KriJ8Sg4lWIoNswKWc4", _updateTodo) | |
/// .then((StreamSubscription s) => _subscriptionTodo = s); | |
static Future<StreamSubscription<Event>> getTodoStream(String todoKey, | |
void onData(Todo todo)) async { | |
String accountKey = await Preferences.getAccountKey(); | |
StreamSubscription<Event> subscription = FirebaseDatabase.instance | |
.reference() | |
.child("accounts") | |
.child(accountKey) | |
.child("todos") | |
.child(todoKey) | |
.onValue | |
.listen((Event event) { | |
var todo = new Todo.fromJson(event.snapshot.key, event.snapshot.value); | |
onData(todo); | |
}); | |
return subscription; | |
} | |
/// FirebaseTodos.getTodo("-KriJ8Sg4lWIoNswKWc4").then(_updateTodo); | |
static Future<Todo> getTodo(String todoKey) async { | |
Completer<Todo> completer = new Completer<Todo>(); | |
String accountKey = await Preferences.getAccountKey(); | |
FirebaseDatabase.instance | |
.reference() | |
.child("accounts") | |
.child(accountKey) | |
.child("todos") | |
.child(todoKey) | |
.once() | |
.then((DataSnapshot snapshot) { | |
var todo = new Todo.fromJson(snapshot.key, snapshot.value); | |
completer.complete(todo); | |
}); | |
return completer.future; | |
} | |
} | |
class Preferences { | |
static const String ACCOUNT_KEY = "accountKey"; | |
static Future<bool> setAccountKey(String accountKey) async { | |
SharedPreferences prefs = await SharedPreferences.getInstance(); | |
prefs.setString(ACCOUNT_KEY, accountKey); | |
return prefs.commit(); | |
} | |
static Future<String> getAccountKey() async { | |
SharedPreferences prefs = await SharedPreferences.getInstance(); | |
String accountKey = prefs.getString(ACCOUNT_KEY); | |
// workaround - simulate a login setting this | |
if (accountKey == null) { | |
accountKey = "-KriFiUADpl-X07hnBC-"; | |
} | |
return accountKey; | |
} | |
} |
Does this work with firestore?
return ListTile(title: Text(snapshot.value['name']));
Error:- The method '[]' can't be unconditionally invoked because the receiver can be 'null'.
Try making the call conditional (using '?.') or adding a null check to the target ('!').
plz solve this
return ListTile(title: Text(snapshot.value['name'])); Error:- The method '[]' can't be unconditionally invoked because the receiver can be 'null'. Try making the call conditional (using '?.') or adding a null check to the target ('!').
plz solve this
Add null check
ex: Text(snapshot.value['name'] ?? " " ),
send me the address of anydesk
…On Sun, 11 Dec 2022 at 21:15, Aditya More ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
return ListTile(title: Text(snapshot.value['name'])); Error:- The method
'[]' can't be unconditionally invoked because the receiver can be 'null'.
Try making the call conditional (using '?.') or adding a null check to the
target ('!').
plz solve this
Add null check
ex: Text(snapshot.value['name']??"",
—
Reply to this email directly, view it on GitHub
<https://gist.github.com/ea80ce71179c41fdd8bbdb796ca889f4#gistcomment-4398964>
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AYGLZGUUUJLGF6FC53IFO2LWMXZIBBFKMF2HI4TJMJ2XIZLTSKBKK5TBNR2WLJDHNFZXJJDOMFWWLK3UNBZGKYLEL52HS4DFQKSXMYLMOVS2I5DSOVS2I3TBNVS3W5DIOJSWCZC7OBQXE5DJMNUXAYLOORPWCY3UNF3GS5DZVRZXKYTKMVRXIX3UPFYGLK2HNFZXIQ3PNVWWK3TUUZ2G64DJMNZZDAVEOR4XAZNEM5UXG5FFOZQWY5LFVA4DQMBUGQYTMOFHORZGSZ3HMVZKMY3SMVQXIZI>
.
You are receiving this email because you commented on the thread.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
line 153:
return prefs.commit();
Problem
'commit' is depriciated,should not be used
please tell the updated command...