实施 Codelab Firebase 聊天示例

Posted

技术标签:

【中文标题】实施 Codelab Firebase 聊天示例【英文标题】:Implementing Codelab Firebase Chat example 【发布时间】:2018-07-03 11:07:22 【问题描述】:

在根据 https://codelabs.developers.google.com/codelabs/flutter-firebase/#8 实现 Firebase for Flutter 时

在“使用 AnimatedList”步骤后遇到错误。一切似乎都符合教程 - Firebase 数据库插件的版本差异是否会导致这种情况?

代码:

new Flexible(
                child: new FirebaseAnimatedList(
                    query: reference,
                    sort: (a, b) => b.key.compareTo(a.key),
                    padding: new EdgeInsets.all(8.0),
                    reverse: true,
                    itemBuilder: (_, DataSnapshot snapshot, Animation<double> animation) 
                      return new MessageBubble(
                          snapshot: snapshot,
                          animation: animation
                      );
                    
                )
            ),

调试控制台:

I/flutter (17175): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (17175): The following assertion was thrown building PageChatConversation(dirty, state:
I/flutter (17175): PageChatConversationState#e4c10):
I/flutter (17175): type '(BuildContext, DataSnapshot, Animation<double>) => dynamic' is not a subtype of type
I/flutter (17175): 'FirebaseAnimatedListItemBuilder' of 'itemBuilder'
I/flutter (17175): 
I/flutter (17175): Either the assertion indicates an error in the framework itself, or we should provide substantially
I/flutter (17175): more information in this error message to help you determine and fix the underlying cause.
I/flutter (17175): In either case, please report this assertion by filing a bug on GitHub:
I/flutter (17175):   https://github.com/flutter/flutter/issues/new
I/flutter (17175): 
I/flutter (17175): When the exception was thrown, this was the stack:
I/flutter (17175): #0      new FirebaseAnimatedList (package:firebase_database/ui/firebase_animated_list.dart:25:20)
I/flutter (17175): #1      PageChatConversationState.build (/data/user/0/com.payitforward.payitforward/cache/pay_it_forwardQZWJQC/pay_it_forward/lib/UI/page_chat_conversation.dart:286:28)
I/flutter (17175): #2      StatefulElement.build (package:flutter/src/widgets/framework.dart:3690:27)
I/flutter (17175): #3      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3602:15)
I/flutter (17175): #4      Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter (17175): #5      ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3582:5)
I/flutter (17175): #6      StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3718:22)
I/flutter (17175): #7      ComponentElement.mount (package:flutter/src/widgets/framework.dart:3577:5)
I/flutter (17175): #8      Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter (17175): #9      Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter (17175): #10     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter (17175): #11     Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter (17175): #12     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3582:5)
I/flutter (17175): #13     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3577:5)
I/flutter (17175): #14     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter (17175): #15     Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter (17175): #16     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter (17175): #17     Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter (17175): #18     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3582:5)
I/flutter (17175): #19     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3577:5)
I/flutter (17175): #20     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter (17175): #21     Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter (17175): #22     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4588:14)
I/flutter (17175): #23     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter (17175): #24     Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter (17175): #25     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4588:14)
I/flutter (17175): #26     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter (17175): #27     Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter (17175): #28     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter (17175): #29     Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter (17175): #30     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3582:5)
I/flutter (17175): #31     StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3718:22)
I/flutter (17175): #32     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3577:5)
I/flutter (17175): #33     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter (17175): #34     Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter (17175): #35     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4588:14)
I/flutter (17175): #36     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter (17175): #37     Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter (17175): #38     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter (17175): #39     Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter (17175): #40     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3582:5)
I/flutter (17175): #41     StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3718:22)
I/flutter (17175): #42     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3577:5)
I/flutter (17175): #43     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter (17175): #44     Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter (17175): #45     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter (17175): #46     Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter (17175): #47     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3582:5)
I/flutter (17175): #48     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3577:5)
I/flutter (17175): #49     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter (17175): #50     Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter (17175): #51     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4588:14)
I/flutter (17175): #52     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter (17175): #53     Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter (17175): #54     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4588:14)
I/flutter (17175): #55     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter (17175): #56     Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter (17175): #57     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter (17175): #58     Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter (17175): #59     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3582:5)
I/flutter (17175): #60     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3577:5)
I/flutter (17175): #61     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter (17175): #62     Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter (17175): #63     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4588:14)
I/flutter (17175): #64     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter (17175): #65     Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter (17175): #66     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter (17175): #67     Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter (17175): #68     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3582:5)
I/flutter (17175): #69     StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3718:22)
I/flutter (17175): #70     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3577:5)
I/flutter (17175): #71     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter (17175): #72     Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter (17175): #73     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter (17175): #74     Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter (17175): #75     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3582:5)
I/flutter (17175): #76     StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3718:22)
I/flutter (17175): #77     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3577:5)
I/flutter (17175): #78     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter (17175): #79     Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter (17175): #80     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter (17175): #81     Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter (17175): #82     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3582:5)
I/flutter (17175): #83     StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3718:22)
I/flutter (17175): #84     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3577:5)
I/flutter (17175): #85     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2875:14)
I/flutter (17175): #86     Element.updateChild (package:flutter/src/widgets/framework.dart:2678:12)
I/flutter (17175): #87     RenderObjectElement.updateChildren (package:flutter/src/widgets/framework.dart:4375:32)
I/flutter (17175): #88     MultiChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4703:17)
I/flutter (17175): #89     Element.updateChild (package:flutter/src/widgets/framework.dart:2667:15)
I/flutter (17175): #90     _TheatreElement.update (package:flutter/src/widgets/overlay.dart:507:16)
I/flutter (17175): #91     Element.updateChild (package:flutter/src/widgets/framework.dart:2667:15)
I/flutter (17175): #92     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter (17175): #93     Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter (17175): #94     StatefulElement.update (package:flutter/src/widgets/framework.dart:3737:5)
I/flutter (17175): #95     Element.updateChild (package:flutter/src/widgets/framework.dart:2667:15)
I/flutter (17175): #96     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter (17175): #97     Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter (17175): #98     ProxyElement.update (package:flutter/src/widgets/framework.dart:3847:5)
I/flutter (17175): #99     Element.updateChild (package:flutter/src/widgets/framework.dart:2667:15)
I/flutter (17175): #100    SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4595:14)
I/flutter (17175): #101    Element.updateChild (package:flutter/src/widgets/framework.dart:2667:15)
I/flutter (17175): #102    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter (17175): #103    Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter (17175): #104    StatefulElement.update (package:flutter/src/widgets/framework.dart:3737:5)
I/flutter (17175): #105    Element.updateChild (package:flutter/src/widgets/framework.dart:2667:15)
I/flutter (17175): #106    SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4595:14)
I/flutter (17175): #107    Element.updateChild (package:flutter/src/widgets/framework.dart:2667:15)
I/flutter (17175): #108    SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4595:14)
I/flutter (17175): #109    Element.updateChild (package:flutter/src/widgets/framework.dart:2667:15)
I/flutter (17175): #110    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3613:16)
I/flutter (17175): #111    Element.rebuild (package:flutter/src/widgets/framework.dart:3463:5)
I/flutter (17175): #112    BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2210:33)
I/flutter (17175): #113    BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&RendererBinding&WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:612:20)
I/flutter (17175): #114    BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:208:5)
I/flutter (17175): #115    BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:931:15)
I/flutter (17175): #116    BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:871:9)
I/flutter (17175): #117    BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:783:5)
I/flutter (17175): #118    _invoke (file:///b/build/slave/Linux_Engine/build/src/flutter/lib/ui/hooks.dart:120)
I/flutter (17175): #119    _drawFrame (file:///b/build/slave/Linux_Engine/build/src/flutter/lib/ui/hooks.dart:109)
I/flutter (17175): ════════════════════════════════════════════════════════════════════════════════════════════════════
I/flutter (17175): Another exception was thrown: type '(BuildContext, DataSnapshot, Animation<double>) => dynamic' is not a subtype of type 'FirebaseAnimatedListItemBuilder' of 'itemBuilder'
I/FlutterActivityDelegate(17175): onResume setting current activity to this
E/FirebaseInstanceId(17175): Token retrieval failed: AUTHENTICATION_FAILED
W/IInputConnectionWrapper(17175): reportFullscreenMode on inexistent InputConnection
I/FlutterActivityDelegate(17175): onResume setting current activity to this

有什么见解吗?提前谢谢!

【问题讨论】:

【参考方案1】:

index 参数丢失

typedef Widget FirebaseAnimatedListItemBuilder(
  BuildContext context,
  DataSnapshot snapshot,
  Animation<double> animation,
  int index,
);

像这样添加

itemBuilder: (_, DataSnapshot snapshot, Animation<double> animation, _) 

(添加, _)应该可以修复错误。

【讨论】:

实际上,添加 ", int index" 修复了它,但很好,谢谢。

以上是关于实施 Codelab Firebase 聊天示例的主要内容,如果未能解决你的问题,请参考以下文章

Firebase iOS Codelab Swift 中的错误

有啥方法可以获取 firebase 请求的状态吗?

Firebase Android 聊天示例:无法弹回输入

输入聊天消息时的Firebase警告[重复]

Google Assistant App Actions codelab 示例在 AndroidTV 上不起作用(Fit Actions)

实施后台位置更新后,FCM 发疯并在没有编程触发的情况下连续发送