learnandro
learnandro

Reputation: 21

ArgumentError (Invalid argument: Instance of 'TextEditingController')

Please help.. i'm trying to make update data page, but this error come out in this line..

Firestore.instance.collection('reg').add({'name':controllerName}) 

here is the code:

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


class EditList extends StatefulWidget {

  @override
  _EditListState createState() => _EditListState();
}

class _EditListState extends State<EditList> {
TextEditingController controllerName;


@override
void initState() { 
  controllerName = new TextEditingController();
  super.initState();

}
  var name;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        title: Text('Registration'),
        backgroundColor: Colors.blue,
      ),
      body: Container(
        child: SingleChildScrollView(
          padding: const EdgeInsets.all(30.0),
          child: Column(
            children: <Widget>[
              Padding(
                padding: const EdgeInsets.only(top: 20.0),
              ),
              Text('GROUP'),
              TextField(
                controller: controllerName,
                  onChanged: (String str) {
                    setState(() {
                      name= str;
                    });
                  },
                  decoration: InputDecoration(
                    labelText: 'Name',
                  )),
              //paste here
              const SizedBox(height: 30),
              RaisedButton(
                onPressed: () {
                  if (controllerName.text.isNotEmpty) {
                    Firestore.instance.collection('reg').add({'name':controllerName}) 
                    .then((result){
                      Navigator.pop(context);
                      controllerName.clear();
                    }).catchError((err) =>print(err));
                  }
                },
                child: const Text('Submit', style: TextStyle(fontSize: 20)),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

Upvotes: 1

Views: 7747

Answers (5)

Iwo Richard
Iwo Richard

Reputation: 1

Change this line

Firestore.instance.collection('reg').add({'name':controllerName})

To this line

Firestore.instance.collection('reg').add({'name':controllerName.text})

The difference is controllerName.text

Upvotes: 0

DataXPH
DataXPH

Reputation: 19

How do I add uuid inside the document? Tried adding .docs(uuid) before .add({ it's having an error.

  CollectionReference users = FirebaseFirestore.instance.collection('users');

  String? uuid = " ";

  Future<void> addUser() {
    FirebaseAuth.instance.authStateChanges().listen((User? user) {
      if (user == null) {
        print('User is currently signed out!');
      } else {
        uuid = user.uid;
        print(uuid);
      }
    });

    // Call the user's CollectionReference to add a new user
    return users
        .add({
          'uuid': uuid, // John Doe
          'first': firstNameController.text, // John Doe
          'middle': middleNameController.text, // Stokes and Sons
          'surname': surNameController.text // 42
        })
        .then((value) => print("User Added"))
        .catchError((error) => print("Failed to add user: $error"));
  }

Upvotes: 1

K_Chandio
K_Chandio

Reputation: 777

Passing TextEditingController will definetly cause error because it just have instance of controller but you need text data to pass to function in upper most line. Controller attached to a textfield contains many of the property along with text inside the textfield. You need to get the text from controller and pass it to the firebase function. The line causing error:

Firestore.instance.collection('reg').add({'name':controllerName}) 

should be like this,

Firestore.instance.collection('reg').add({'name':controllerName.text}) 

and will work for sure.

Upvotes: 0

Sonu kumar
Sonu kumar

Reputation: 441

controllerName is not a String,

controllerName.text

use that

Upvotes: 1

Benjamin
Benjamin

Reputation: 6171

This line:

Firestore.instance.collection('reg').add({'name':controllerName})

should be replaced with:

Firestore.instance.collection('reg').add({'name':controllerName.text})

Also, you should probably give your TextField an initial value of an empty string so that it can't be null.

Upvotes: 4

Related Questions