使用firebase在颤振中构建聊天屏幕时出错
向所有人致意, 我正在学习使用firebase在Flatter应用程序中进行聊天,我得到了一个代码来理解它的工作原理,但它显示错误,我无法很好地理解代码。 下面是显示错误的屏幕-:使用firebase在颤振中构建聊天屏幕时出错,firebase,flutter,firebase-cloud-messaging,flutter-layout,chat,Firebase,Flutter,Firebase Cloud Messaging,Flutter Layout,Chat,向所有人致意, 我正在学习使用firebase在Flatter应用程序中进行聊天,我得到了一个代码来理解它的工作原理,但它显示错误,我无法很好地理解代码。 下面是显示错误的屏幕-: import 'dart:async'; import 'dart:io'; import 'dart:core'; import 'package:firebase_analytics/firebase_analytics.dart'; import 'package:firebase_auth/firebase_
import 'dart:async';
import 'dart:io';
import 'dart:core';
import 'package:firebase_analytics/firebase_analytics.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_database/firebase_database.dart';
import 'package:firebase_database/ui/firebase_animated_list.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_chat_app/widget/ChatMessageListItem.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:image_picker/image_picker.dart';
final googleSignIn = new GoogleSignIn();
final analytics = new FirebaseAnalytics();
final auth = FirebaseAuth.instance;
var currentUserEmail;
var _scaffoldContext;
class ChatScreen extends StatefulWidget {
@override
ChatScreenState createState() {
return new ChatScreenState();
}
}
class ChatScreenState extends State<ChatScreen> {
final TextEditingController _textEditingController =
new TextEditingController();
bool _isComposingMessage = false;
final reference = FirebaseDatabase.instance.reference().child('messages');
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("Flutter Chat App"),
elevation:
Theme.of(context).platform == TargetPlatform.iOS ? 0.0 : 4.0,
actions: <Widget>[
new IconButton(
icon: new Icon(Icons.exit_to_app), onPressed: _signOut)
],
),
body: new Container(
child: new Column(
children: <Widget>[
new Flexible(
child: new FirebaseAnimatedList(
query: reference,
padding: const EdgeInsets.all(8.0),
reverse: true,
sort: (a, b) => b.key.compareTo(a.key),
//comparing timestamp of messages to check which one would appear first
// this itembuilder is showing error
itemBuilder: (_,DataSnapshot messageSnapshot, Animation<double> animation){
return new ChatMessageListItem(
messageSnapshot: messageSnapshot,
animation: animation,
);
},
),
),
new Divider(height: 1.0),
new Container(
decoration:
new BoxDecoration(color: Theme.of(context).cardColor),
child: _buildTextComposer(),
),
new Builder(builder: (BuildContext context) {
_scaffoldContext = context;
return new Container(width: 0.0, height: 0.0);
}),
],
),
decoration: Theme.of(context).platform == TargetPlatform.iOS
? new BoxDecoration(
border: new Border(
top: new BorderSide(
color: Colors.grey[200],
)))
: null,
),
);
}
CupertinoButton getIOSSendButton() {
return new CupertinoButton(
child: new Text("Send"),
onPressed: _isComposingMessage
? () => _textMessageSubmitted(_textEditingController.text)
: null,
);
}
IconButton getDefaultSendButton() {
return new IconButton(
icon: new Icon(Icons.send),
onPressed: _isComposingMessage
? () => _textMessageSubmitted(_textEditingController.text)
: null,
);
}
Widget _buildTextComposer() {
return new IconTheme(
data: new IconThemeData(
color: _isComposingMessage
? Theme.of(context).accentColor
: Theme.of(context).disabledColor,
),
child: new Container(
margin: const EdgeInsets.symmetric(horizontal: 8.0),
child: new Row(
children: <Widget>[
new Container(
margin: new EdgeInsets.symmetric(horizontal: 4.0),
child: new IconButton(
icon: new Icon(
Icons.photo_camera,
color: Theme.of(context).accentColor,
),
onPressed: () async {
await _ensureLoggedIn();
File imageFile = await ImagePicker.pickImage();
int timestamp = new DateTime.now().millisecondsSinceEpoch;
Reference storageReference = FirebaseStorage
.instance
.ref()
.child("img_" + timestamp.toString() + ".jpg");
var uploadTask =
storageReference.putFile(imageFile);
// this downloadUrl is also showing an error
Uri downloadUrl = (uploadTask.snapshotEvents).downloadUrl;
_sendMessage(
messageText: null, imageUrl: downloadUrl.toString());
}
),
),
new Flexible(
child: new TextField(
controller: _textEditingController,
onChanged: (String messageText) {
setState(() {
_isComposingMessage = messageText.length > 0;
});
},
onSubmitted: _textMessageSubmitted,
decoration:
new InputDecoration.collapsed(hintText: "Send a message"),
),
),
new Container(
margin: const EdgeInsets.symmetric(horizontal: 4.0),
child: Theme.of(context).platform == TargetPlatform.iOS
? getIOSSendButton()
: getDefaultSendButton(),
),
],
),
));
}
Future<Null> _textMessageSubmitted(String text) async {
_textEditingController.clear();
setState(() {
_isComposingMessage = false;
});
await _ensureLoggedIn();
_sendMessage(messageText: text, imageUrl: null);
}
void _sendMessage({String messageText, String imageUrl}) {
reference.push().set({
'text': messageText,
'email': googleSignIn.currentUser.email,
'imageUrl': imageUrl,
'senderName': googleSignIn.currentUser.displayName,
'senderPhotoUrl': googleSignIn.currentUser.photoUrl,
});
analytics.logEvent(name: 'send_message');
}
Future<Null> _ensureLoggedIn() async {
GoogleSignInAccount signedInUser = googleSignIn.currentUser;
if (signedInUser == null)
signedInUser = await googleSignIn.signInSilently();
if (signedInUser == null) {
await googleSignIn.signIn();
analytics.logLogin();
}
currentUserEmail = googleSignIn.currentUser.email;
if (await auth.currentUser == null) {
GoogleSignInAuthentication credentials =
await googleSignIn.currentUser.authentication;
// here sign in with google is also showing error.
await auth.signInWithGoogle(
idToken: credentials.idToken, accessToken: credentials.accessToken);
}
}
Future _signOut() async {
await auth.signOut();
googleSignIn.signOut();
Scaffold
.of(_scaffoldContext)
.showSnackBar(new SnackBar(content: new Text('User logged out')));
}
}
导入'dart:async';
导入“dart:io”;
导入“省道:核心”;
导入“包:firebase_analytics/firebase_analytics.dart”;
导入“包:firebase_auth/firebase_auth.dart”;
导入“package:firebase_database/firebase_database.dart”;
导入“package:firebase_database/ui/firebase_animated_list.dart”;
导入“包:firebase_存储/firebase_存储.dart”;
进口“包装:颤振/cupertino.dart”;
进口“包装:颤振/材料.省道”;
导入“package:flatter_chat_app/widget/ChatMessageListItem.dart”;
导入“包:google_sign_in/google_sign_in.dart”;
导入“包:image_picker/image_picker.dart”;
最终谷歌签名=新谷歌签名();
最终分析=新FirebaseAnalytics();
final auth=FirebaseAuth.instance;
var-currentUserEmail;
var_scaffoldContext;
类ChatScreen扩展StatefulWidget{
@凌驾
ChatScreenState createState(){
返回新的ChatScreenState();
}
}
类ChatScreenState扩展了状态{
最终文本编辑控制器\u文本编辑控制器=
新建TextEditingController();
bool_isComposingMessage=false;
final reference=FirebaseDatabase.instance.reference().child('messages');
@凌驾
小部件构建(构建上下文){
归还新脚手架(
appBar:新的appBar(
标题:新文本(“颤振聊天应用程序”),
高程:
Theme.of(context.platform==TargetPlatform.iOS?0.0:4.0,
行动:[
新图标按钮(
图标:新图标(图标。退出到应用程序),按下时:\注销)
],
),
主体:新容器(
子:新列(
儿童:[
新柔性(
子级:新的FirebaseAnimatedList(
查询:参考,
填充:常数边集全部(8.0),
相反:是的,
排序:(a,b)=>b.key.compareTo(a.key),
//比较消息的时间戳以检查哪一个将首先出现
//此itembuilder显示错误
itemBuilder:(u、DataSnapshot消息快照、动画){
返回新的ChatMessageListItem(
messageSnapshot:messageSnapshot,
动画:动画,
);
},
),
),
新隔板(高度:1.0),
新容器(
装饰:
新的盒子装饰(颜色:主题.of(上下文).cardColor),
子项:_buildTextComposer(),
),
新建生成器(生成器:(BuildContext上下文){
_脚手架上下文=上下文;
返回新容器(宽度:0.0,高度:0.0);
}),
],
),
装饰:Theme.of(context.platform==TargetPlatform.iOS
?新的盒子装饰(
边界:新边界(
顶部:新边界(
颜色:颜色。灰色[200],
)))
:null,
),
);
}
CupertinoButton GetIOSSENDBUTON()按钮{
返回新的CupertinoButton(
子项:新文本(“发送”),
onPressed:\u是复合消息
?()=>\u textMessageSubmitted(\u textEditingController.text)
:null,
);
}
IconButton getDefaultSendButton(){
返回新图标按钮(
图标:新图标(Icons.send),
onPressed:\u是复合消息
?()=>\u textMessageSubmitted(\u textEditingController.text)
:null,
);
}
小部件_buildTextComposer(){
返回新的IconTheme(
数据:新的IconThemeData(
颜色:\是复合信息
主题。背景。强调颜色
:Theme.of(context).disabledColor,
),
子容器:新容器(
边距:常量边集。对称(水平:8.0),
孩子:新的一排(
儿童:[
新容器(
边距:新边集。对称(水平:4.0),
孩子:新的图标按钮(
图标:新图标(
图标。照相/摄像机,
颜色:主题。背景。强调颜色,
),
onPressed:()异步{
等待_ensureLoggedIn();
File imageFile=wait ImagePicker.pickImage();
int timestamp=new DateTime.now().millissecondssinceepoch;
Reference-storageReference=FirebaseStorage
.例如
.ref()
.child(“img_”+timestamp.toString()+”.jpg”);
var上传任务=
storageReference.putFile(imageFile);
//此下载URL也显示错误
Uri downloadUrl=(uploadTask.snapshotEvents);
_发送消息(
messageText:null,imageUrl:downloadUrl.toString();
}
),
),
新柔性(
孩子:新文本字段(
控制器:_textededitingcontroller,
onChanged:(字符串messageText){
设置状态(){
_isComposingMessage=messageText.length>0;
});
},
onSubmitted:_textMessageSubmitted,
装饰:
新建InputDecoration.collapsed(hintText:“发送消息”),
),
),
新容器(
边距:常量边集。对称(水平:4.0),
itemBuilder: (_,DataSnapshot messageSnapshot, Animation<double> animation, int yourVariableName)
{
// Put your code here
}