1

I have created a sqlite crud application from flutter. my app data insert into the table successfully. but when i show that inserted date from listview need to reload app.in my app data insert from a separate screen. data have shown on home screen. i want show data without reload app in listview how can I slove my problem?

Here is my app code. this is my home screen. this screen show data from listview.

FutureBuilder<List<Student>>(
                    future: DatabaseHelper.instance.getStudentDetails(),
                    builder: (BuildContext context,
                        AsyncSnapshot<List<Student>> snapshot) {
                      if (!snapshot.hasData) {
                        return Center(child: Text('Loading...'));
                      }
                      return snapshot.data!.isEmpty
                          ? Center(child: Text('No Student Details in List.'))
                          : ListView(
                              children: snapshot.data!.map((student) {
                                return Center(
                                  child: Card(
                                    color: selectedId == student.id
                                        ? Colors.white70
                                        : Colors.white,
                                    child: ListTile(
                                      title: Text(
                                          'Student Name : ${student.name}'),
                                      subtitle:
                                          Text('Course : ${student.course}'),
                                      onTap: () {},
                                      onLongPress: () {
                                        setState(() {
                                          DatabaseHelper.instance
                                              .remove(student.id!);
                                        });
                                      },
                                    ),
                                  ),
                                );
                              }).toList(),
                            );
                    }),

This is my second screen.this screen I adding data insert into sqlite table.when I click floating action button I want to show insert data in home screen list view without reload.

Column(
        children: [
          SizedBox(
            height: 10.0,
          ),
          Text(
            'Enter Student Details',
            style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.bold),
          ),
          Padding(
            padding: const EdgeInsets.only(top: 8.0),
            child: Divider(color: Colors.black),
          ),
          Padding(
            padding: const EdgeInsets.all(8.0),
            child: TextField(
              decoration: InputDecoration(
                label: Text('Enter Name :'),
              ),
              controller: nameController,
            ),
          ),
          Padding(
            padding: const EdgeInsets.all(8.0),
            child: TextField(
              decoration: InputDecoration(
                label: Text('Enter Course :'),
              ),
              controller: courseController,
            ),
          ),
        ],
      ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.save),
        onPressed: () async {
          await DatabaseHelper.instance.add(
            Student(name: nameController.text, course: courseController.text),
          );
          setState(() {
            nameController.clear();
            courseController.clear();
            selectedId = null;
          });
        },
      ),
Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
sameera
  • 23
  • 5

1 Answers1

0

future: DatabaseHelper.instance.getStudentDetails(), will get recall the api on every setState(build).

To avoid recalling the api, create a state variable for this future on state class (outside the build method).

late final future = DatabaseHelper.instance.getStudentDetails();

and use it on FutureBuilder

FutureBuilder<List<Student>>(
    future: future,
    builder: (BuildContext context,

You can check Fixing a common FutureBuilder and StreamBuilder problem by Randal L. Schwartz.

Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56