我有一个包含来自服务器的数据的页面。我通过BloC接收数据。我还有一个按钮来删除数据。当我删除数据时,应该是stateElectricvehicles.id == 0,另一个页面应该会出现。但是我遇到了一个问题,当我删除数据时,它们仍然在屏幕上,我需要重新访问页面,这样数据就消失了。告诉我如何才能在删除数据之后,立即更新页面,使数据消失,而不需要重新访问该页?
Widget _child(Size size, BuildContext context, double topPadding) {
return BlocBuilder<MycarsCubit, MycarsState>(
builder: (context, stateElectricvehicles) {
final ElectricvehiclesCubit cubit =
BlocProvider.of<ElectricvehiclesCubit>(context);
if (stateElectricvehicles is MycarsInitial) {
carNumber.text = stateElectricvehicles.number;
carNumber.selection = TextSelection.fromPosition(
TextPosition(offset: carNumber.text.length),
);
if (stateElectricvehicles.id == 0) {
savedCarId = stateElectricvehicles.id;
}
Future.delayed(const Duration(seconds: 3), () {
if (mounted) {
setState(() {
isLoading = false;
});
}
});
final SharedPrefs prefs = SharedPrefs();
return FutureBuilder<int?>(
future: prefs.getInt('USER_ID'),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return const SizedBox();
}
return Padding(
padding: EdgeInsets.only(top: topPadding + 10),
child: Column(
children: [
Padding(
padding: const EdgeInsets.only(left: 30, right: 30),
child: BackstepWidget(
text: 'My EVs',
onBackPressed: () {
Routemaster.of(context).history.back();
},
),
),
const SizedBox(height: 10),
snapshot.data != null
? CarList(
savedCarId: savedCarId,
onIndexChanged: (value) {
savedCarId = value;
},
)
: const SizedBox(),
const SizedBox(height: 30),
stateElectricvehicles.id == 0
? isLoading
? const CircularProgressIndicator(color: constants.Colors.purpleMain)
: const EmptyDetails()
: snapshot.data != null
? Expanded(
child: SingleChildScrollView(
child: Column(
children: [
GestureDetector(
onTap: () {
showDialog(
context: context,
builder: (context) {
return RemoveCarDialog(
removed:
(value) {
if (value) {
setState(
() {
stateElectricvehicles
.carModel =
null;
stateElectricvehicles
.id = 0;
stateElectricvehicles
.number = '';
stateElectricvehicles
.type = '';
});
}
},
state:
stateElectricvehicles,
);
},
).then((value) {
cubit.fetchCars(
snapshot.data ??
-1,
);
final MycarsCubit
myCars =
BlocProvider.of<
MycarsCubit>(
context);
myCars.clear();
});
},cubit
class MycarsCubit extends Cubit<MycarsState> {
MycarsCubit()
: super(MycarsInitial(
model: '',
number: '',
type: '',
id: 0,
));
String number = '';
String type = '';
int id = 0;
late CarModel? carModel;
void clear() {
number = '';
type = '';
id = 0;
carModel = null;
}
void change({
required String numberText,
required String typeText,
required int idText,
required CarModel? carModelNew,
}) {
id = idText;
number = numberText;
type = typeText;
carModel = carModelNew;
emit(MycarsInitial(
number: number,
type: type,
id: id,
carModel: carModel,
));
}
}状态
abstract class MycarsState {}
class MycarsInitial extends MycarsState {
int id;
String number;
String type;
CarModel? carModel;
MycarsInitial({
required this.id,
required this.number,
this.carModel,
required this.type,
});
}发布于 2022-08-29 20:22:55
你必须发出新的状态,你只需改变你的Cubit类中的变量。
试着在你的净空中发出:
void clear() {
emit(MycarsInitial(
number: "",
type: "",
id: 0,
carModel: null,
));
}https://stackoverflow.com/questions/73534216
复制相似问题