Last active
January 3, 2025 23:25
-
-
Save melnikovdv/0c1e6abeffdbccfb708d5f01a3489a9d to your computer and use it in GitHub Desktop.
app
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 'dart:io'; | |
import 'package:flutter/material.dart'; | |
import 'package:firebase_core/firebase_core.dart'; | |
import 'package:firebase_database/firebase_database.dart'; | |
Future<void> main() async { | |
final FirebaseApp app = await FirebaseApp.configure( | |
name: 'db2', | |
options: Platform.isIOS | |
? const FirebaseOptions( | |
googleAppID: 'todo', | |
gcmSenderID: 'todo', | |
databaseURL: 'todo', | |
) | |
: const FirebaseOptions( | |
googleAppID: '###', | |
apiKey: '###', | |
databaseURL: 'https://###.firebaseio.com', | |
), | |
); | |
runApp(new MyApp(app)); | |
} | |
class MyApp extends StatelessWidget { | |
MyApp(this.app); | |
final FirebaseApp app; | |
// This widget is the root of your application. | |
@override | |
Widget build(BuildContext context) { | |
return new MaterialApp( | |
title: 'Events app', | |
theme: new ThemeData( | |
// This is the theme of your application. | |
// | |
// Try running your application with "flutter run". You'll see the | |
// application has a blue toolbar. Then, without quitting the app, try | |
// changing the primarySwatch below to Colors.green and then invoke | |
// "hot reload" (press "r" in the console where you ran "flutter run", | |
// or press Run > Flutter Hot Reload in IntelliJ). Notice that the | |
// counter didn't reset back to zero; the application is not restarted. | |
primarySwatch: Colors.blue, | |
), | |
home: new MyHomePage(title: 'Events Demo Home Page', app: app), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
MyHomePage({Key key, this.title, this.app}) : super(key: key); | |
final FirebaseApp app; | |
final String title; | |
@override | |
_MyHomePageState createState() => new _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
int _counter = 0; | |
DatabaseReference _counterRef; | |
StreamSubscription<Event> _counterSubscription; | |
DatabaseError _error; | |
@override | |
void initState() { | |
super.initState(); | |
final FirebaseDatabase database = new FirebaseDatabase(app: widget.app); | |
database.setPersistenceEnabled(true); | |
database.setPersistenceCacheSizeBytes(10000000); | |
print("!!!"); | |
_counterRef = database.reference().child('test'); | |
_counterRef.keepSynced(true); | |
_counterSubscription = _counterRef.onValue.listen((Event event) { | |
setState(() { | |
_error = null; | |
_counter = event.snapshot.value; | |
print("Counter " + _counter.toString()); | |
}); | |
}, onError: (Object o) { | |
final DatabaseError error = o; | |
setState(() { | |
_error = error; | |
}); | |
}); | |
} | |
void _increment() async { | |
print("increment"); | |
// Increment counter in transaction. | |
final TransactionResult transactionResult = | |
await _counterRef.runTransaction((MutableData mutableData) async { | |
mutableData.value = (mutableData.value ?? 0) + 1; | |
return mutableData; | |
}); | |
if (transactionResult.committed) { | |
print('Transaction commited'); | |
} else { | |
print('Transaction not committed.'); | |
if (transactionResult.error != null) { | |
print(transactionResult.error.message); | |
} | |
} | |
} | |
@override | |
void dispose() { | |
_counterSubscription.cancel(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
// This method is rerun every time setState is called, for instance as done | |
// by the _incrementCounter method above. | |
// | |
// The Flutter framework has been optimized to make rerunning build methods | |
// fast, so that you can just rebuild anything that needs updating rather | |
// than having to individually change instances of widgets. | |
return new Scaffold( | |
appBar: new AppBar( | |
// Here we take the value from the MyHomePage object that was created by | |
// the App.build method, and use it to set our appbar title. | |
title: new Text(widget.title), | |
), | |
body: new Center( | |
// Center is a layout widget. It takes a single child and positions it | |
// in the middle of the parent. | |
child: new Column( | |
// Column is also layout widget. It takes a list of children and | |
// arranges them vertically. By default, it sizes itself to fit its | |
// children horizontally, and tries to be as tall as its parent. | |
// | |
// Invoke "debug paint" (press "p" in the console where you ran | |
// "flutter run", or select "Toggle Debug Paint" from the Flutter tool | |
// window in IntelliJ) to see the wireframe for each widget. | |
// | |
// Column has various properties to control how it sizes itself and | |
// how it positions its children. Here we use mainAxisAlignment to | |
// center the children vertically; the main axis here is the vertical | |
// axis because Columns are vertical (the cross axis would be | |
// horizontal). | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
new Text( | |
'You have pushed the button this many times:', | |
), | |
new Text( | |
'$_counter', | |
style: Theme.of(context).textTheme.display1, | |
), | |
], | |
), | |
), | |
floatingActionButton: new FloatingActionButton( | |
onPressed: _increment, | |
tooltip: 'Increment', | |
child: new Icon(Icons.add), | |
), // This trailing comma makes auto-formatting nicer for build methods. | |
); | |
} | |
} |
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
name: events | |
description: Events app | |
dependencies: | |
flutter: | |
sdk: flutter | |
# The following adds the Cupertino Icons font to your application. | |
# Use with the CupertinoIcons class for iOS style icons. | |
cupertino_icons: ^0.1.0 | |
firebase_core: ^0.2.3 | |
firebase_database: ^0.4.5 | |
firebase_auth: ^0.5.5 | |
google_sign_in: ^3.0.2 | |
dev_dependencies: | |
flutter_test: | |
sdk: flutter | |
# For information on the generic Dart part of this file, see the | |
# following page: https://www.dartlang.org/tools/pub/pubspec | |
# The following section is specific to Flutter. | |
flutter: | |
# The following line ensures that the Material Icons font is | |
# included with your application, so that you can use the icons in | |
# the material Icons class. | |
uses-material-design: true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment