我正在尝试获取该网站的数据,我的代码是否有问题,或者这是所请求的网站方面的问题?
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flash_chat/constants.dart';
import 'package:flutter/material.dart';
final _firestore = FirebaseFirestore.instance;
User loggedinUser;
class ChatScreen extends StatefulWidget {
static const String route = '/chat';
@override
_ChatScreenState createState() => _ChatScreenState();
}
class _ChatScreenState extends State<ChatScreen> {
final _auth = FirebaseAuth.instance;
final messageTextController =
TextEditingController(); // to remove the sent message from the textbox
String messageText;
void getCurrentUser() async {
/*
* Default is null; as user logs in it contains some value
* */
try {
final user = await _auth.currentUser;
if (user != null) {
loggedinUser = user;
loggedinUser.email;
//print('res************************************: $recentLoggedInUser');
}
} catch (e) {}
}
// void getMessages() async {
// final messages = await _firestore.collection('messages').get();
// for (var message in messages.docs) {
// print(message.data());
// }
// }
// void messagesStream() async {
// await for (var snapshot in _firestore.collection('messages').snapshots()) {
// for (var message in snapshot.docs) {
// print(message.data());
// }
// }
// }
@override
void initState() {
// TODO: implement initState
super.initState();
getCurrentUser();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
leading: null,
actions: <Widget>[
IconButton(
icon: Icon(Icons.close),
onPressed: () {
// messagesStream();
_auth.signOut();
Navigator.pop(context);
}),
],
title: Text('⚡️Chat'),
backgroundColor: Colors.lightBlueAccent,
),
body: SafeArea(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
messageStreams(), // Just get the stream here to populate
Container(
decoration: kMessageContainerDecoration,
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Expanded(
child: TextField(
controller:
messageTextController, // this controller handels the text after being sent
onChanged: (value) {
messageText = value;
},
decoration: kMessageTextFieldDecoration,
),
),
FlatButton(
onPressed: () {
/*
* add method receives a map that accepts <String, dynamic> format;
* field name should be same as defined into firebase portal
* */
// use the controller to clear the text box as soon as it is sent
messageTextController.clear();
_firestore.collection('messages').add({
'text': messageText,
'sender': loggedinUser.email,
});
},
child: Text(
'Send',
style: kSendButtonTextStyle,
),
),
],
),
),
],
),
),
);
}
}
class messageStreams extends StatelessWidget {
@override
Widget build(BuildContext context) {
return StreamBuilder<QuerySnapshot>(
stream: _firestore.collection('messages').snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Center(
child: CircularProgressIndicator(
backgroundColor: Colors.lightBlueAccent,
),
);
}
final messages = snapshot.data.docs.reversed;
// .reversed; // reversed make a list in reversed order, so newest will appear at lowest
List<messageBubble> messagewidgets = [];
for (var m in messages) {
final messageText = m.get('text');
final senderText = m.get('sender'); // get sender email from DB
var recentLogger = loggedinUser.email;
CrossAxisAlignment c = recentLogger == senderText
? CrossAxisAlignment.end
: CrossAxisAlignment.start;
final messageWidgit = messageBubble(
sender: senderText,
text: messageText,
crossAxisAlignments: c,
);
messagewidgets.add(messageWidgit);
}
/*
* As we have Container, we will use expanded, this way it will take only the part that is necessary
* */
return Expanded(
child: ListView(
reverse: true,
padding: EdgeInsets.symmetric(
horizontal: 10.0,
vertical: 20.0,
),
children: messagewidgets,
),
);
},
);
}
}
class messageBubble extends StatelessWidget {
final sender;
final text;
CrossAxisAlignment crossAxisAlignments;
messageBubble({this.sender, this.text, this.crossAxisAlignments});
@override
Widget build(BuildContext context) {
BorderRadius br;
Color clr;
if (crossAxisAlignments == CrossAxisAlignment.start) {
br = BorderRadius.only(
topRight: Radius.circular(30.0),
bottomLeft: Radius.circular(30.0),
bottomRight: Radius.circular(30.0),
);
clr = Colors.lightBlueAccent;
} else if (crossAxisAlignments == CrossAxisAlignment.end) {
br = BorderRadius.only(
topLeft: Radius.circular(30.0),
bottomLeft: Radius.circular(30.0),
bottomRight: Radius.circular(30.0),
);
clr = Colors.blueAccent;
}
return Padding(
padding: EdgeInsets.all(10.0),
child: Column(
crossAxisAlignment: crossAxisAlignments,
children: <Widget>[
Text(
sender,
style: TextStyle(
fontSize: 12.0,
color: Colors.black54,
),
),
BuildChatList(br, clr),
],
),
);
}
Material BuildChatList(BorderRadius br, Color clr) {
return Material(
borderRadius: br,
elevation: 5.0,
color: clr,
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 10.0, horizontal: 20.0),
child: Text(
'$text',
style: TextStyle(
color: Colors.white,
fontSize: 15.0,
),
),
),
);
}
}
//BorderRadius.only(
//topLeft: Radius.circular(30.0),
//bottomLeft: Radius.circular(30.0),
//bottomRight: Radius.circular(30.0),
//)
我得到的不是得到数据: