Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UpdateValue in List Cubit Flutter

Tags:

flutter

dart

bloc

In app i am using Cubit. ItemData fetch from firestore. Everything works, but after added item in list and update value(name) in firestore, in list still old value. How to solve it?

 class TestPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          BlocBuilder<ItemCubit, ItemState>(
            cubit: ItemCubit(DataBase())..getItemData(item),
            builder: (context, state) {
              if (state is ItemData) {
                return Column(
                  children: [
                    Text(state.item.name),
                    RaisedButton(
                      onPressed: () {
                        Navigator.push(
                            context,
                            MaterialPageRoute(
                                builder: (context) => TestPage1(
                                      item: state.item,
                                    )));
                      },
                      child: Text('showPage'),
                    ),
                    RaisedButton(
                      onPressed: () {
                        context.bloc<TestCubit>().add(item);
                      },
                    )
                  ],
                );
              }
              return Container(
                child: Text('error'),
              );
            },
          )
        ],
      ),
    );
  }
}

for add item in list i am uisng another cubit code:

class AddCubit extends Cubit<AddState> {
  AddCubit() : super(AddInitial());
  List<Item> items = List<Item>();

  void addItem(Item item) {
    items.add(item);
    emit(LoadList(items));
  }
}

This is bloc for retrieve list of items in TestPage1:

  BlocBuilder<AddCubit, AddState>(builder: (context, state) {
          if (state is LoadList) {
            return Column(
              children: state.items.toSet().map((item) {
                return Card(
                  child: Text(item.name),
                );
              }).toList(),
            );
          }
        })

state code:

class LoadList extends AddState {
  final List<Item> items;

  LoadList(this.items);
}
like image 583
Andrey Khan Avatar asked Dec 29 '25 12:12

Andrey Khan


1 Answers

In flutter when you compare two objects of the same class, you will have always equality even if the values of them are different. Unless you will use equality method in your class.

Class code with equality method

import 'package:equatable/equatable.dart';

class LoadList extends AddState {
  final List<Item> items;

  LoadList(this.items);
  @override
  List<Object> get props => [items];
}

Second thing is the fact that u should use copy with and don't create new state for new value. It will come handy later and reduce the number of possible errors later on.

Whole code for state class

import 'package:equatable/equatable.dart';

class LoadList extends AddState {
  final List<Item> items;
  LoadList(this.items);

  LoadList copyWith({
    List<Item> items,
  }) {
    return LoadList(
      items: items?? this.items,
    );
  }

  @override
  List<Object> get props => [items];
}

And then for your void function you should use:

void addItem(Item item) {
    items.add(item);
    emit(state.copyWith(items: items);
 }
like image 102
Goldlightdrake Avatar answered Jan 01 '26 01:01

Goldlightdrake



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!