Flutter:Firebase Realtime从对象列表中删除对象

Flutter:Firebase Realtime从对象列表中删除对象,第1张

概述我正在咨询数据库中注册的所有俱乐部.对于每个俱乐部,我将其添加到对象列表中. 当该人删除俱乐部时,会从数据库中删除该俱乐部,但在项目列表中未删除,我尝试执行以下 *** 作: 我的NotClub-Player.dart课程 // FIREBASE CLUBSList<Club> items = List();Club item;DatabaseReference itemRef;@overrid 我正在咨询数据库中注册的所有俱乐部.对于每个俱乐部,我将其添加到对象列表中.

当该人删除俱乐部时,会从数据库中删除该俱乐部,但在项目列表中未删除,我尝试执行以下 *** 作:

我的NotClub-Player.dart课程

// FIREBASE CLUBSList<Club> items = List();Club item;DatabaseReference itemRef;@overrIDevoID initState() {   super.initState();   item = Club("","",false,"");   final FirebaseDatabase database = FirebaseDatabase.instance;   itemRef = database.reference().child(player.player_game_platform).child("CLUB");   itemRef.onChildAdded.Listen(_onEntryAdded);   itemRef.onChildRemoved.Listen(_onEntryRemoved);   itemRef.onChildChanged.Listen(_onEntryChanged);}// CLUBS ListENERS_onEntryAdded(Event event) { setState(() {   items.add(Club.fromSnapshot(event.snapshot)); });}_onEntryRemoved(Event event) { setState(() {   items.remove(Club.fromSnapshot(event.snapshot)); });}_onEntryChanged(Event event) { var old = items.singleWhere((entry) {   return entry.key == event.snapshot.key; }); setState(() {   items[items.indexOf(old)] = Club.fromSnapshot(event.snapshot); });}

我的问题:
在_onEntryRemoved中有一个事件.在那种情况下,它返回已删除的项目.但它不会从列表中删除相应的项目.

在数据库中,它已成功删除.但是包含该对象的列表尚未删除它.

这是我的查询

rnew Flexible(            child: new FirebaseAnimatedList(              query: FirebaseDatabase.instance.reference().child(player.player_game_platform).child("CLUB").orderByChild("club_name"),itemBuilder: (BuildContext context,DataSnapshot snapshot,Animation<double> animation,int index) {                return new Column(                  children: <Widget>[                    new Container(                      decoration: new Boxdecoration(                        color: colors.grey[300],),child: new ListTile(                        leading: new CachednetworkImage(imageUrl: items[index].club_logo,wIDth: 60.0),Title: new Text(items[index].club_name,style: new TextStyle(color: colors.black)),subTitle: new Text("CAPTAIN: "+items[index].club_captain,trailing: new Raisedbutton(                            color: colors.lightBlue[500],child: new Text("JOIN",style: new TextStyle(color: colors.white)),onpressed: (){                            }                        ),new divIDer(                      color: colors.grey[700],height: 0.0,],);              },

这是我在_onEntryRemoved中得到的
– 它返回正确的删除,但删除列表不适用于我.

E/Flutter (15214): [ERROR:topaz/lib/tonic/logging/dart_error.cc(16)] Unhandled exception:E/Flutter (15214): setState() called after dispose(): _join_clubState#a99f1(lifecycle state: defunct,not mounted)E/Flutter (15214): This error happens if you call setState() on a State object for a Widget that no longer appears in the Widget tree (e.g.,whose parent Widget no longer includes the Widget in its build). This error can occur when code calls setState() from a timer or an animation callback. The preferred solution is to cancel the timer or stop Listening to the animation in the dispose() callback. Another solution is to check the "mounted" property of this object before calling setState() to ensure the object is still in the tree.E/Flutter (15214): This error might indicate a memory leak if setState() is being called because another object is retaining a reference to this State object after it has been removed from the tree. To avoID memory leaks,consIDer breaking the reference to this object during dispose().E/Flutter (15214): #0      State.setState.<anonymous closure> (package:Flutter/src/Widgets/framework.dart:1098:9)E/Flutter (15214): #1      State.setState (package:Flutter/src/Widgets/framework.dart:1124:6)E/Flutter (15214): #2      _join_clubState._onEntryRemoved (package:proclubscommunity/Club-Player.dart:807:5)E/Flutter (15214): #3      _RootZone.runUnaryguarded (dart:async/zone.dart:1316:10)E/Flutter (15214): #4      _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:330:11)E/Flutter (15214): #5      _DelayedData.perform (dart:async/stream_impl.dart:578:14)E/Flutter (15214): #6      _StreamImplEvents.handleNext (dart:async/stream_impl.dart:694:11)E/Flutter (15214): #7      _PendingEvents.schedule.<anonymous closure> (dart:async/stream_impl.dart:654:7)E/Flutter (15214): #8      _microtaskLoop (dart:async/schedule_microtask.dart:41:21)E/Flutter (15214): #9      _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)I/Flutter (15214): {club_logo: url_image,club_note:,club_since:,club_three_position:,club_market: false,club_market_color:,club_six_position:,club_premium: false,club_twitter: https://www.twitter.com/,club_first_position:,club_category: 0,club_seven_position:,club_description:,club_copa: 0,club_country: ESPAÑA,club_liga: 0,club_four_position:,club_logo_file_name: club_logo.png,club_plataform: PS4,club_nine_position:,club_captain: RaiiLKilleR,club_name: barcelona,club_five_position:,club_eight_position:,club_ID: 1,club_second_position:,club_logo_folder_name: PS4_barcelona,club_twitch: https://www.twitch.tv/,club_youtube: https://www.youtube.com/}

添加键到小部件:

return new Column(              key: new ObjectKey(items[index].club_name),children: <Widget>[                new Container(                  decoration: new Boxdecoration(                    color: colors.grey[300],child: new ListTile(                    leading: new CachednetworkImage(imageUrl: items[index].club_logo,subTitle: new Text("Captain: "+items[index].club_captain,trailing: new Raisedbutton(                        color: colors.lightBlue[500],onpressed: (){                        }                    ),new divIDer(                  color: colors.grey[700],);

_onEntryRemoved()

_onEntryRemoved(Event event) {    setState(() {      print(event.snapshot.value['club_name']);      items.remove(event.snapshot.value['club_name']);    });  }
解决方法 你的问题在于俱乐部.

当您创建List< Club>时项目和以后执行items.remove(clubInstance),内部remove方法将使用标准对象equals方法实现,这是不知道你的密钥.

如果您尝试使用items.indexOf(clubInstance),也会发生同样的情况.它永远不会“找到”该项目,总是返回-1.

您可以更改实现,遍历项目以确切地找出需要删除的项目,然后将其删除,或者您可以在Club类中实现==和hashCode.

如果您打算使用club_name作为键,添加这两行可能会解决它.

class Club {  Club({this.club_name});  final String club_name;  // this is what you would have to add to your class:  bool operator ==(o) => o is Club && o.club_name == club_name;  int get hashCode => club_name.hashCode;}

注意:这与颤动或火焰无关,这只是飞镖!

总结

以上是内存溢出为你收集整理的Flutter:Firebase Realtime从对象列表中删除对象全部内容,希望文章能够帮你解决Flutter:Firebase Realtime从对象列表中删除对象所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1000553.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存