Home > OS >  Data is not being saved at Firebase nor is being retrieved from the firebase
Data is not being saved at Firebase nor is being retrieved from the firebase

Time:10-26

There was no problem saving data into firebase until today. I think this is the same code i use most of the time to store the data in the firebase but strangely the collection " LoanFriends" is not being saved at the firebase. I may be missing some minute details. The following code doesn't generate any error in the android studio but the data is not being saved.

The code is as follows:

import 'package:budgetapp/ExtractedWidgets/dialog_field.dart';
import 'package:budgetapp/constant.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:cloud_firestore/cloud_firestore.dart';


class WalletScreen extends StatelessWidget {
static String id = 'wallet_screen';

TextEditingController _controllername = TextEditingController();
TextEditingController _controlleraddress =TextEditingController();
TextEditingController _controllerphoneno = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        title: Text('My Wallet',style: kheadertext),
        elevation: 0,
        actions: [IconButton(onPressed: (){}, icon: Icon(Icons.notifications_sharp),),],
      ),
      body: Column(
        children: [
          Container(
              height: 200,
              decoration: containerdecoration.copyWith( borderRadius: BorderRadius.circular(32.0),),
              margin: EdgeInsets.all(15),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Expanded(
                    child: Container(
                      padding: EdgeInsets.only(left: 35),
                      child: Row(
                        children: [
                          Text('Balance',style: kmyTextstyle.copyWith(fontSize: 25),),
                          SizedBox(width: 200,),
                          Icon(FontAwesomeIcons.moneyBill,
                            color: Colors.white,),
                        ],
                      ),
                    ),
                  ),
                  Expanded(
                    child: Container(
                      padding: EdgeInsets.only(left: 35),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: [
                          Text('Rs. ', style: kmyTextstyle.copyWith(fontSize: 35),),
                          SizedBox(width: 5,),
                          Text("25000",style: kmyTextstyle.copyWith(fontSize: 35),),
                        ],),
                    ),
                  ),
                  Expanded(
                    child: Container(
                      padding: EdgeInsets.only(left: 35),
                      child: Row(
                        children: [
                          Text('My wallet information',style: kmyTextstyle.copyWith(fontSize: 15),),
                          SizedBox(width: 150,),
                          Icon(FontAwesomeIcons.ccMastercard,
                            color: Colors.white,),
                        ],
                      ),
                    ),
                  ),
                ],
              )
          ),
          Text("Loan Mechanism",style: kheadertext.copyWith(fontSize: 20),),
          SizedBox(height: 8,),
          StreamBuilder<QuerySnapshot?>(
              stream: FirebaseFirestore.instance.collection('LoanFriends').snapshots(),
              builder: (context,snapshot){
                if(!snapshot.hasData){
                  return CircularProgressIndicator();
                }
                else {
                  return ListView.builder(
                      shrinkWrap: true,
                      itemCount: snapshot.data!.docs.length,
                      itemBuilder:(context, index){
                        return Text(snapshot.data!.docs[index]['name']);
                      });
                }
              }
          ),
        ],
      ),
      floatingActionButton: FloatingActionButton(
        backgroundColor: Color(0xFFcdc3ff),
        onPressed: (){
          showAlertDialog(context);
        },
        child: Icon(Icons.add),
      ),
    );
  }

  void showAlertDialog(BuildContext context) {
    showDialog(context: context, builder: (context){
      return AlertDialog(
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(32), ),
        content: Container(
          height: MediaQuery.of(context).size.height*0.4,
          width: MediaQuery.of(context).size.width*0.5,
          child: Column(
            children: [
              Dialogfield(hinttext: 'Enter name', controller: _controllername, textInputtype: TextInputType.name),
              Dialogfield(hinttext: 'Enter address', controller: _controlleraddress, textInputtype: TextInputType.text),
              Dialogfield(hinttext: 'Enter phonenumber', controller: _controllerphoneno, textInputtype: TextInputType.number),
              TextButton(
                style: ButtonStyle(
                  backgroundColor: MaterialStateProperty.all<Color>(
                      Color(0xFFcdc3ff)),
                ),
                onPressed: (){
                Map<String, dynamic> mapdata = {'name': _controllername, 'address': _controlleraddress, 'phonenumber': _controllerphoneno
                };
                FirebaseFirestore.instance.collection('LoanFriends').add(mapdata);
                _controllername.text ='';
                _controlleraddress.text='';
                _controllerphoneno.text ='';
                Navigator.pop(context);
              },
                child: Text('Add User',style: kmyTextstyle.copyWith(fontSize: 15),),),
            ],
          ),
        ),
      );
    });
  }
}

CodePudding user response:

You need to access the text property of a controller. You cant refer to controller directly.

Change this line:

Map<String, dynamic> mapdata = {'name': _controllername, 'address': _controlleraddress, 'phonenumber': _controllerphoneno

into this line:

Map<String, dynamic> mapdata = {'name': _controllername.text, 'address': _controlleraddress.text, 'phonenumber': _controllerphoneno.text
  • Related