0
votes

My edit class.

import 'package:finalyearproject/model/NewUser.dart';
import 'package:finalyearproject/service/database.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:uuid/uuid.dart';




class UpdateSupervisor extends StatefulWidget {

  final NewUser sv;

  UpdateSupervisor({Key key, this.sv}) : super(key: key);

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


class _UpdateSupervisorState extends State<UpdateSupervisor> {
  final GlobalKey<FormState> _formKey = GlobalKey();
  TextEditingController _name;
  TextEditingController _email;
  TextEditingController _nophone;
  TextEditingController _uniqueID;


  var uuid = Uuid();

@override
void initState(){
     super.initState();
     _name = TextEditingController(text:  widget.sv.name);
     _email = TextEditingController(text:  widget.sv.email);
     _nophone = TextEditingController(text: widget.sv.nophone);
     _uniqueID= TextEditingController(text: widget.sv.uniqueID);
  }
    @override
    Widget build(BuildContext context) {
      return Scaffold(
          appBar: AppBar(
            title: Text('Edit Supervisor'),
            backgroundColor: Colors.redAccent,
          ),
          body: Form(
            key: _formKey,
            child: SingleChildScrollView(
              padding: const EdgeInsets.all(16.0),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.end,
                children: <Widget>[
                  SizedBox(height: 25.0),
                  TextFormField(
                    decoration: InputDecoration(
                        hintText: 'Name',
                        border: OutlineInputBorder(
                            borderRadius: BorderRadius.circular(5))),
                    keyboardType: TextInputType.text,
                    controller: _name,
                  ),
                  SizedBox(height: 10.0),
                  TextFormField(
                    decoration: InputDecoration(
                        hintText: 'Email',
                        border: OutlineInputBorder(
                            borderRadius: BorderRadius.circular(5))),
                    keyboardType: TextInputType.emailAddress,
                    controller: _email,

                  ),
                  SizedBox(height: 10.0),
                  TextFormField(
                    decoration: InputDecoration(
                        hintText: 'Number Phone',
                        border: OutlineInputBorder(
                            borderRadius: BorderRadius.circular(5))),
                    keyboardType: TextInputType.number,
                    controller: _nophone,

                  ),
                  SizedBox(height: 10.0),
                  TextFormField(
                    decoration: InputDecoration(
                        hintText: 'Unique ID ',
                        border: OutlineInputBorder(
                            borderRadius: BorderRadius.circular(5))),
                    keyboardType: TextInputType.number,
                    controller: _uniqueID,
                  ),
                  const SizedBox(height: 20.0),
                  RaisedButton(
                      color: Colors.redAccent,
                      textColor: Colors.black,
                      child: Text( "Update"),
                      onPressed: () async {
                        if (_formKey.currentState.validate()) {
                          DatabaseService().updateData(NewUser(name: _name.text, email: _email.text, nophone: _nophone.text, uniqueID: _uniqueID.text, id: uuid.v4()));
                        }
                      }
                  ),
                ],
              ),
            ),
          )
      );
    }
  }

My function update

 // update data supervisor
    Future updateData(NewUser newUser) async {
     
        return supervisorCollection.document(newUser.id).updateData(
            newUser.toJson()).whenComplete(() {
          print("Update success!");
        });
    }

This is my console firebase

supervisor collection

here is my error

Unhandled Exception: PlatformException(Error performing updateData, NOT_FOUND: No document to update: projects/finalyearproject-5c689/databases/(default)/documents/Supervisor/3dbac00b-29ad-4357-a480-e38a21c90042, null)

I had try using setData() for my update function but it will add new data, not updating the existing data. So someone can help me to solve this problem? is there anything that i missing ?

1

1 Answers

0
votes

uuid.v4() will generate a v4 (random) id and you are trying to update a document with this new randomly generated ID which does not exist in your collection.

to fix it:

var id = widget.sv.id;

use this id in your new user to send to update function:

DatabaseService().updateData(NewUser(name: _name.text, email: _email.text, nophone: _nophone.text, uniqueID: _uniqueID.text, id: id));