使用提供程序flutter添加和删除列表中的项目



我正在使用provider制作包含用户喜爱的旅程的收藏夹列表,然后我将在喜爱的旅程屏幕中显示这些旅程。

Favorite.dart:

import 'package:flutter/material.dart';
class Favorite extends ChangeNotifier {
final Text date;
final Text time;
final Text source;
final Text destination;
final Text price;
Favorite(this.date, this.time, this.source, this.destination, this.price);
}
class Following extends ChangeNotifier {
List<Favorite> list = [];
add(favorite) {
list.add(favorite);
notifyListeners();
}
remove(favorite) {
list.remove(favorite);
notifyListeners();
}
}

travels.dart(显示所有行程(:

FirebaseAnimatedList(
shrinkWrap: true,
query: Consts.journeyRef.child("journeys"),
itemBuilder: (BuildContext context, DataSnapshot snapshot,
Animation animation, int index) {
try {
return Consumer<Following>(
builder:
(BuildContext context, value, child) {
return Dismissible(
key: UniqueKey(),
secondaryBackground: buildSwipeActionRight(),
background: buildSwipeActionLeft(),
child: ListView(
scrollDirection: Axis.vertical,
shrinkWrap: true,
children: <Widget>[
eachTile(
following,
Favorite(
Text(Map<String, dynamic>.from(
snapshot.value as Map)[Consts.pathDateJourney]),
Text(Map<String, dynamic>.from(
snapshot.value as Map)[Consts.pathTimeJourney]),
Text(Map<String, dynamic>.from(
snapshot.value as Map)[Consts.pathSourceCity]),
Text(Map<String, dynamic>.from(
snapshot.value as Map)[Consts.pathDestinationCity]),
Text(Map<String, dynamic>.from(
snapshot.value as Map)[Consts.pathPriceJourney]),),)

]),
onDismissed: (direction) =>
dismissItem(context, index, direction),
);
},
);
} catch (e) {
customSnackBar(context, e.toString(), 3, Colors.white24, Colors.brown, 17);
return const Text(
"We Can't show you information disabled by the Administrator");
}
}),

eachTile.dart:

ListTile eachTile(following, favorite) {
return ListTile(
leading: Column(
children: [
favorite.date,
const SizedBox(
height: 10,
),
favorite.time,
],
),
title: Row(
children: [
favorite.source,
const SizedBox(
width: 50,
),
favorite.price
],
),
subtitle: favorite.destination,
trailing: IconButton(
icon: following.list.contains(favorite)
? const Icon(Icons.favorite)
: const Icon(Icons.favorite_border_outlined),
onPressed: () {
print(following.list.contains(favorite));
// this print statement always false
if (following.list.contains(favorite)) {
following.remove(favorite);
} else {
following.add(favorite);
}
print(following.list.contains(favorite));
// this print statement always true
print(following.list);
// this print statement print the list and in each time the code execute new instance added to the list
},
),
);
}

这段代码可以很好地将旅程添加到列表中,但问题是当你再次点击最喜欢的图标时,条件

following.list.contains(favorite)

返回false(这意味着这个对象不在列表中,但这是错误的。我试图打印列表,但有一个实例(下面的实例似乎发生了变化,但我没有创建任何新实例。我认为它每次都在创建新的不同实例。

使用提供程序向收藏夹列表添加和删除项目的最佳方式是什么?

输出:

I/flutter (  578): false
I/flutter (  578): true
I/flutter (  578): [Instance of 'Favorite']
V/AutofillManager(  578): requestHideFillUi(null): anchor = null
I/flutter (  578): false
I/flutter (  578): true
I/flutter (  578): [Instance of 'Favorite', Instance of 'Favorite']
V/AutofillManager(  578): requestHideFillUi(null): anchor = null
I/flutter (  578): false
I/flutter (  578): true
I/flutter (  578): [Instance of 'Favorite', Instance of 'Favorite', Instance of 'Favorite']

首先在worked.dart中清理这里的代码。如果你使用了提供者的两种方法,你可以通过归档这个任务

1.你可以使用consumer widget或provider.of(context(,但你同时使用这两种方式只调用Following provider

2.在旅行.dart中,如果你决定使用consumer,那么在(BuildContext context, Following value, Widget? child)中,你可以直接使用value和child,而不需要在前面写数据类型,例如(BuildContext context, value, child)

3.你能显示你的控制台输出吗

问题是,我正在使两个对象之间的运算符等于,但它返回false,这是因为除了字符串、int和double等原始数据类型之外,dart语言中的所有对象。。。彼此不相等,因为no==运算符被重写并设置为它,这还包括列表、映射等集合。。。解决方案是覆盖Favorite类中的==运算符

class Favorite{
final Text date;
final Text time;
final Text source;
final Text destination;
final Text price;
Favorite(this.date, this.time, this.source, this.destination, this.price);@override
bool operator == (Object other) {
return other is Favorite && date.toString() == other.date.toString() && time.toString() == other.time.toString() && source.toString() == other.source.toString() && destination.toString() == other.destination.toString() && price.toString() == other.price.toString();
}
}

现在,当我在listTile Widget中运行following.list.contains(favorite)或下面的.list[0]==收藏夹时,它将返回true。就是

每次进行更改时,都应该调用notifyListeners();。一个示例实现是:
在您的提供者类内部:

void add(int n) {
myList.add(n);
notifyListeners();
}

最新更新