manan36chauhan / Topics-For-Flutter

only informative
0 stars 0 forks source link

Firestore data fetch #3

Open Manan-dynamic opened 1 year ago

Manan-dynamic commented 1 year ago
import 'package:cloud_firestore/cloud_firestore.dart';

class DatabaseService {
  Future<String?> addUser({
    required String fullName,
    required String age,
    required String email,
  }) async {
    try {
      CollectionReference users =
          FirebaseFirestore.instance.collection('users');
      // Call the user's CollectionReference to add a new user
      await users.doc(email).set({
        'full_name': fullName,
        'age': age,
      });
      return 'success';
    } catch (e) {
      return 'Error adding user';
    }
  }

  Future<String?> getUser(String email) async {
    try {
      CollectionReference users =
          FirebaseFirestore.instance.collection('users');
      final snapshot = await users.doc(email).get();
      final data = snapshot.data() as Map<String, dynamic>;
      return data['full_name'];
    } catch (e) {
      return 'Error fetching user';
    }
  }
}
Manan-dynamic commented 1 year ago

https://medium.com/google-developer-experts/firestore-database-flutter-38c9a0cc77c7

https://mukhtharcm.com/listview-from-firestore/

https://github.com/bradtraversy/design-resources-for-developers

manan36chauhan commented 1 year ago

Streambuilder listview firestore db ui

import 'dart:async';
//import 'package:transparent_image/transparent_image.dart';
//import'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:flutter/material.dart';

class Streambuil extends StatefulWidget {
  const Streambuil({Key? key}) : super(key: key);

  @override
  State<Streambuil> createState() => _StreambuilState();
}

class _StreambuilState extends State<Streambuil> {
  int count = 0;
  StreamController controllerStream = StreamController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Center(child: StreamBuilder(
            stream: controllerStream.stream,
            builder: (context, snapshot) {
          if (snapshot.hasData) {
            return Text(
              snapshot.data.toString(),
              style: TextStyle(fontSize: 25),
            );
          }else{
            return Text("0",style: TextStyle(fontSize: 25),);
          }
        })),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: (){
          count++;
          controllerStream.sink.add(count);
        },
        child: Text('+'),
      ),
    );
  }
}