Installation
Add Get to your pubspec.yaml file:
Add Get to your pubspec.yaml file:
get: ^3.22.2
https://pub.dev/packages/get/install
Import get in files that will be used.
import 'package:get/get.dart';Tips: #1
...............................................................................................................................................................................................................................
Example :#1[]
import 'package:flutter/material.dart';
import 'package:get/get.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return GetMaterialApp(
home: Home(),
);
}
}
class Home extends StatelessWidget {
final CounterController controller = Get.put(CounterController());
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Hello'),
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: () => controller.increment(),
),
body: Center(
child: Obx(() => Text( '${controller.count}', )),
),
);
}
}
class CounterController extends GetxController {
var count = 0.obs;
void increment() => count++;
}
Happy Coding :)
...............................................................................................................................................................................................................................
Example :#2[]
Happy Coding :)
...............................................................................................................................................................................................................................
Example :#3[ ]
import 'dart:async';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
Counter counter = Get.put(Counter());
int co;
@override
void initState() {
super.initState();
counter.startStream();
}
void AddData() {
Map<String, int> demodate = {
"count": (co + 1).toInt(),
};
DocumentReference documentReference =
FirebaseFirestore.instance.collection('data').doc('counterState');
documentReference.update(demodate);
}
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () => AddData(),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Center(
child: StreamBuilder(
//stream: counter.streamController.stream,
stream: counter.streamController.stream,
builder: (context, snapshot) {
co = snapshot.data;
return Text('Data: $co');
},
),
)
],
),
),
);
}
}
class Counter extends GetxController {
StreamController<int> streamController = StreamController<int>();
Stream<DocumentSnapshot> pathStream = FirebaseFirestore.instance
.collection('data')
.doc('counterState')
.snapshots();
void startStream() {
pathStream.listen((event) {
streamController.sink.add(event.data()['count']);
});
}
@override
void onClose() {
streamController.close();
}
}
....................
import 'package:flutter/material.dart';
import 'package:getProject/screens/home.dart';
import 'package:get/get.dart';
import 'package:firebase_core/firebase_core.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return GetMaterialApp(
home: Home(),
);
}
}
Happy Coding :)
...............................................................................................................................................................................................................................
Example :#4[]
Happy Coding :)
No comments:
Post a Comment