Home > Enterprise >  Add list of details to firebase from textformfield?
Add list of details to firebase from textformfield?

Time:08-25

I'am creating a student mark-sheet. I'm getting the student name list from the firebase then show in the ListView.builder it returns the card view in the card view I add the textformfied for enter the student marks. Now my problem is I have only one Controller. I want to store the details in firebase How add the student name and student marks in firebase list.

class AddMarks extends StatefulWidget {
  AddMarks(
      {Key? key,
      required this.title,
      required this.sclName,
      required this.classsec,
      required this.testName,
      required this.totalMarks})
      : super(key: key);
  final String title;
  final String sclName;
  final String classsec;
  final String testName;
  final String totalMarks;

  @override
  State<AddMarks> createState() => _AddMarksState();
}

class _AddMarksState extends State<AddMarks> {
  @override
  initState() {
    super.initState();
    getStudents();
  }

  TextEditingController controller = TextEditingController();
  var studentlist = [];
  var studentNamelist = [];
  Future<void> getStudents() async {
    var students = await FirebaseFirestore.instance
        .collection("Schools")
        .doc(widget.sclName)
        .collection("Class")
        .doc(widget.classsec)
        .get();
    studentlist = students['studentlist'];
    print("===============> Students : "   students['studentlist'].toString());
    print("===============> Students : "   studentlist.toString());
    for (var studentName in studentlist) {
      var studentNames = await FirebaseFirestore.instance
          .collection("Schools")
          .doc(widget.sclName)
          .collection("Students")
          .doc(studentName)
          .get();
      print(studentNames['Student-Name']);
      studentNamelist.add(studentNames['Student-Name']);
    }
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.testName   " - "   widget.title),
      ),
      body: Column(
        children: [
          Container(
            height: 500,
            padding: EdgeInsets.all(12.0),
            child:
            ListView.builder(
              itemCount: studentNamelist.length,
              itemBuilder: (BuildContext context, int index) {
                return Container(
                  height: 60,
                  child: Card(
                    color: Colors.grey.shade200,
                    child: Row(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Container(
                          margin: EdgeInsets.only(top: 10, left: 20),
                          child: Text(
                            studentNamelist[index],
                            style: TextStyle(fontSize: 20),
                          ),
                        ),
                        Container(
                          decoration: BoxDecoration(
                            color: Colors.grey,
                            borderRadius:
                                BorderRadius.all(Radius.circular(5)),
                          ),
                          height: 40,
                          width: 110,
                          margin:
                              EdgeInsets.only(top: 5, left: 20, right: 30),
                          child: Center(
                            child: TextFormField(
                              keyboardType: TextInputType.number,
                              inputFormatters: [
                                LengthLimitingTextInputFormatter(3),
                              ],
                              style: TextStyle(fontSize: 20),
                              decoration: InputDecoration(
                                suffixText: "/ "   widget.totalMarks,
                                border: InputBorder.none,
                                contentPadding:
                                    EdgeInsets.only(left: 15, bottom: 8),
                              ),
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                );
              },
            ),
          ),
          TextButton(onPressed: (){}, child: Text("Add Marks"))
        ],
      ),
    );
  }
}

CodePudding user response:

Create a new List named studentMarks

List<String> studentMarks = [];

When you add the textfield, add an onChanged event too and save it in the list like

TextField(
 onChanged: (val){
   studentMarks.insert(index, "$val");
 }
)

The List studentMarks will hold scores of each student.

Also take a look at how to extract widgets and use its variables. (just a suggestion)

EDIT

class AddMarks extends StatefulWidget {
  AddMarks(
      {Key? key,
      required this.title,
      required this.sclName,
      required this.classsec,
      required this.testName,
      required this.totalMarks})
      : super(key: key);
  final String title;
  final String sclName;
  final String classsec;
  final String testName;
  final String totalMarks;

  @override
  State<AddMarks> createState() => _AddMarksState();
}

class _AddMarksState extends State<AddMarks> {
  @override
  initState() {
    super.initState();
    getStudents();
  }

  TextEditingController controller = TextEditingController();
  var studentlist = [];
  var studentNamelist = [];
  List<String> studentMarks = []; //<--add var here
  Future<void> getStudents() async {
    var students = await FirebaseFirestore.instance
        .collection("Schools")
        .doc(widget.sclName)
        .collection("Class")
        .doc(widget.classsec)
        .get();
    studentlist = students['studentlist'];
    print("===============> Students : "   students['studentlist'].toString());
    print("===============> Students : "   studentlist.toString());
    for (var studentName in studentlist) {
      var studentNames = await FirebaseFirestore.instance
          .collection("Schools")
          .doc(widget.sclName)
          .collection("Students")
          .doc(studentName)
          .get();
      print(studentNames['Student-Name']);
      studentNamelist.add(studentNames['Student-Name']);
      studentMarks.add("");//<---add this
    }
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.testName   " - "   widget.title),
      ),
      body: Column(
        children: [
          Container(
            height: 500,
            padding: EdgeInsets.all(12.0),
            child:
            ListView.builder(
              itemCount: studentNamelist.length,
              itemBuilder: (BuildContext context, int index) {
                return Container(
                  height: 60,
                  child: Card(
                    color: Colors.grey.shade200,
                    child: Row(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Container(
                          margin: EdgeInsets.only(top: 10, left: 20),
                          child: Text(
                            studentNamelist[index],
                            style: TextStyle(fontSize: 20),
                          ),
                        ),
                        Container(
                          decoration: BoxDecoration(
                            color: Colors.grey,
                            borderRadius:
                                BorderRadius.all(Radius.circular(5)),
                          ),
                          height: 40,
                          width: 110,
                          margin:
                              EdgeInsets.only(top: 5, left: 20, right: 30),
                          child: Center(
                            child: TextFormField(
//Add these codes
                              onChanged:(val){
                                studentMarks[index] = val;
                              },
//Add these codes above
                              keyboardType: TextInputType.number,
                              inputFormatters: [
                                LengthLimitingTextInputFormatter(3),
                              ],
                              style: TextStyle(fontSize: 20),
                              decoration: InputDecoration(
                                suffixText: "/ "   widget.totalMarks,
                                border: InputBorder.none,
                                contentPadding:
                                    EdgeInsets.only(left: 15, bottom: 8),
                              ),
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                );
              },
            ),
          ),
          TextButton(onPressed: (){}, child: Text("Add Marks"))
        ],
      ),
    );
  }
}

CodePudding user response:

 TextEditingController controller = TextEditingController();

Add your controller inside your listviewbuilder

   class AddMarks extends StatefulWidget {
      AddMarks(
          {Key? key,
          required this.title,
          required this.sclName,
          required this.classsec,
          required this.testName,
          required this.totalMarks})
          : super(key: key);
      final String title;
      final String sclName;
      final String classsec;
      final String testName;
      final String totalMarks;
    
      @override
      State<AddMarks> createState() => _AddMarksState();
    }
    
    class _AddMarksState extends State<AddMarks> {
      @override
      initState() {
        super.initState();
        getStudents();
      }
    
      var studentlist = [];
      var studentNamelist = [];
      Future<void> getStudents() async {
        var students = await FirebaseFirestore.instance
            .collection("Schools")
            .doc(widget.sclName)
            .collection("Class")
            .doc(widget.classsec)
            .get();
        studentlist = students['studentlist'];
        print("===============> Students : "   students['studentlist'].toString());
        print("===============> Students : "   studentlist.toString());
        for (var studentName in studentlist) {
          var studentNames = await FirebaseFirestore.instance
              .collection("Schools")
              .doc(widget.sclName)
              .collection("Students")
              .doc(studentName)
              .get();
          print(studentNames['Student-Name']);
          studentNamelist.add(studentNames['Student-Name']);
        }
        setState(() {});
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text(widget.testName   " - "   widget.title),
          ),
          body: Column(
            children: [
              Container(
                height: 500,
                padding: EdgeInsets.all(12.0),
                child:
                ListView.builder(
                  itemCount: studentNamelist.length,
                  itemBuilder: (BuildContext context, int index) {
                   TextEditingController controller = TextEditingController();

                    return Container(
                      height: 60,
                      child: Card(
                        color: Colors.grey.shade200,
                        child: Row(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Container(
                              margin: EdgeInsets.only(top: 10, left: 20),
                              child: Text(
                                studentNamelist[index],
                                style: TextStyle(fontSize: 20),
                              ),
                            ),
                            Container(
                              decoration: BoxDecoration(
                                color: Colors.grey,
                                borderRadius:
                                    BorderRadius.all(Radius.circular(5)),
                              ),
                              height: 40,
                              width: 110,
                              margin:
                                  EdgeInsets.only(top: 5, left: 20, right: 30),
                              child: Center(
                                child: TextFormField(
                                  onChanged:(val){
                                     studentMarks.insert(index, controller.text);
                                     },
                                  keyboardType: TextInputType.number,
                                  inputFormatters: [
                                    LengthLimitingTextInputFormatter(3),
                                  ],
                                  style: TextStyle(fontSize: 20),
                                  decoration: InputDecoration(
                                    suffixText: "/ "   widget.totalMarks,
                                    border: InputBorder.none,
                                    contentPadding:
                                        EdgeInsets.only(left: 15, bottom: 8),
                                  ),
                                ),
                              ),
                            ),
                          ],
                        ),
                      ),
                    );
                  },
                ),
              ),
              TextButton(onPressed: (){}, child: Text("Add Marks"))
            ],
          ),
        );
      }
    }
  • Related