Home > Net >  I can't handle data on firebase firestore with flutter. (add data, delete data, update data, re
I can't handle data on firebase firestore with flutter. (add data, delete data, update data, re

Time:07-25

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

class TeacherTable extends StatelessWidget {
  TeacherTable({Key? key}) : super(key: key);

  FirebaseFirestore _firestore = FirebaseFirestore.instance;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          children: [
            ElevatedButton(
                onPressed: () => addData,
                child: const Text('Add data with ADD')),
            ElevatedButton(
                onPressed: () => addDataWithSet,
                child: const Text('Add data with SET')),
            ElevatedButton(
                onPressed: () => dataUpgrade(),
                child: const Text('Upgrade data')),
          ],
        ),
      ),
    );
  }

  addData() async {
    Map<String, dynamic> user = <String, dynamic>{};
    user['name'] = 'Can';
    user['height'] = 185;
    user['age'] = 44;
    await _firestore.collection('users').add(user);
  }

  addDataWithSet() async {
    await _firestore
        .doc('asd/yo0kha0UZMXUKQZOxOpC')
        .set({'school': 'collage'}, SetOptions(merge: true));
  }

  dataUpgrade() async {
    await _firestore.doc('asd/yo0kha0UZMXUKQZOxOpC').update({
      'asd': 'dsa',
    });
  }
}

I've just started to learn about Firestore operations. So I created a few buttons and tried to do the firestore operations. But when I press the buttons nothing happens.

 <script type="module">
// Import the functions you need from the SDKs you need
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.9.1/firebase-app.js";
import { getAnalytics } from "https://www.gstatic.com/firebasejs/9.9.1/firebase-analytics.js";


const firebaseConfig = {
  // my config settings
};

// Initialize Firebase
const app = initializeApp(firebaseConfig);
const analytics = getAnalytics(app);

I'm building a web application. Do I need to add something in index.html? I added cloud_firestore in pubspec.yaml.

CodePudding user response:

Add parenthesis and await to function calls:

children: [
            ElevatedButton(
                onPressed: () => await addData(),
                child: const Text('Add data with ADD')),
            ElevatedButton(
                onPressed: () => await addDataWithSet(),
                child: const Text('Add data with SET')),
            ElevatedButton(
                onPressed: () => await dataUpgrade(),
                child: const Text('Upgrade data')),
          ],

Also, make sure you configured Firebase and added the json files to each platform folder.

CodePudding user response:

Firebase services need some configuration to identify your project hosted on your account.

flutterfire_cli is the recommended way to add firebase to your project. It will add the required configuration for you. You don't need to add/change anything anywhere. You can read more about it in the documentation here

  • Related