Home > Enterprise >  set state is changing the value for few seconds only
set state is changing the value for few seconds only

Time:08-07

i have a Screen.dart and a customListview.dart

i make a stream builder in screen.dart which give me a list of persons then calling customlistview(persons).

i want to make a select button for every person when it is selected the select button change to "selected"

but unfortunately when i press on select button it changed to selected for few seconds then it changed to select

and i dont know the reason!!

Screen.dart

StreamBuilder(
                      stream: DatabaseService().getPersons(),
                      builder:(context, snapshot){
                        if(!snapshot.hasData)
                          return Container();
                        List<Person> persons = snapshot.data as  List<Person>;
                        return Expanded(
                        child: Stack(
                            children: [
                                Container(child: CustomListView_Provider(
                                  
                            ],
                        ),
                    );}
                  )

customListView.dart

 @override
    Widget build(BuildContext context) {
        return ListView.builder(
            itemCount: widget.persons!.length,
            itemBuilder: (context, int currentIndex){
                return Stack(
                    children: <Widget>[
                        Container(
                            width:double.infinity,
                            color: Colors.grey[50],
                            child: createViewItem(widget.persons![currentIndex], context, currentIndex))
                    ],
                );
            },
        );
    }

Widget createViewItem(Person person, BuildContext context, index) {
        return Container(
            child: ListTile(
                title: Card(
                    elevation: 0,
                    child: Container(
                        color: Colors.grey[50],
                        width: 100,
                        child: Column(
                            children: <Widget>[
                               Text(person.Name),
                                Container(
            child: TextButton(
                child: Text(
                    selectedPerson == person ? "Selected" : "Select",
                ),
                onPressed: () => {
                  setState((){
                    selectedPerson  = person;
                  }),
                },
            )
        );
                            ],
                        ),
                    ),
                ),
            )
        );
    }

CodePudding user response:

Whenever state changes, API calls again. Create a state variable for stream like

  late final myStream = DatabaseService().getPersons();

And provide it on

StreamBuilder(
  stream: myStream ,

You can check Fixing a common FutureBuilder and StreamBuilder problem

  • Related