0
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:provider/provider.dart';


void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
}

class Test extends StatelessWidget {
  //final db = FirebaseFirestore.instance;

//Here I have set my own controller name
  TextEditingController sampledata1 = new TextEditingController();
  TextEditingController sampledata2 = new TextEditingController();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Test'),
        centerTitle: true,
        backgroundColor: Colors.red[600],
      ),
      body: Container(
        padding: EdgeInsets.all(40.0),
        child: Center(
          child: Column(
            children: <Widget> [
              TextFormField(
                controller: sampledata1,
                decoration: InputDecoration(
                    hintText: "Simple data 1"
                ),
              ),
              SizedBox(height: 10.0,),
              TextFormField(
                controller: sampledata2,
                decoration: InputDecoration(
                    hintText: "Simple data 2"
                ),
              ),
              SizedBox(height: 10.0,),
              FlatButton(
                onPressed: () async {
//Here I am trying to add document on firestore with users uid but could not get any solution
                  final uid = await Provider.of(context).auth.getCurrentUID();
                 Map <String, dynamic> data = {"field1": sampledata1.text, "field2": sampledata2.text};
                  //FirebaseFirestore.instance.collection("UserData").add(data);
                },
                child: Text('Submit'),
                color: Colors.red[600],
              )
            ],
          ),
        ),
      ),
    );
  }
}

2 Answers 2

2

When you call add(...) Firestore auto-generates an ID for the new document for you.

If you want to control the ID yourself, use doc(uid).set(...):

FirebaseFirestore.instance.collection("UserData").doc(uid).set(data);
2
  • Thank u for that I got it but now nothing is displaying on my firestore.. I think I have pass my uid wrong.... any idea on idea on how to pass it correctly...I'm using Google account to log in to my system Feb 15, 2021 at 2:53
  • 1
    I focused my answer on how to use a value you determine as the document ID. If your code for looking up the UID is not working, you should be able to confirm that in the debugger by checking the value of uid after this line executes final uid = await Provider.of(context).auth.getCurrentUID();. Feb 15, 2021 at 3:00
1

Im updating your onPress method, Just replace it and it will work

             FlatButton(
                onPressed: () async {
                  final currentUid = await Provider.of(context).auth.getCurrentUID();
                 Map <String, dynamic> data = {"field1": sampledata1.text, "field2": sampledata2.text};
                  await FirebaseFirestore.instance.collection("UserData").doc(currentUid).add(data);
                },
                child: Text('Submit'),
                color: Colors.red[600],
              )

If it works then please approve my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.