'StramBuilder not working , with no errors. FLUTTER
hey guys I'm new to Flutter, and I'm turning to build a very simple chat app for learning purposes, I'm using StreamBulider for that, it shows me no errors, but it's not working
I tread to add Text Widget on its own without the streambuilder so I can make sure that the problem with the streambulder function not anything else and it's works fine
can anyone help find what went wrong?
here is my code
import 'package:flutter/material.dart';
import 'package:flash_chat/constants.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
class ChatScreen extends StatefulWidget {
static String id = 'chat_screen';
@override
_ChatScreenState createState() => _ChatScreenState();
}
class _ChatScreenState extends State<ChatScreen> {
final _fireStore = FirebaseFirestore.instance;
final _auth = FirebaseAuth.instance;
late String messegeText;
late User loogedInUser;
@override
void initState() {
super.initState();
getCurrentUser();
}
//------------------------------------------------------------------------------
// GetCurrentUser Method...
void getCurrentUser() async {
try {
// ignore: await_only_futures
final user = await _auth.currentUser;
if (user != null) {
loogedInUser = user;
}
} catch (e) {
print(e);
}
}
//------------------------------------------------------------------------------
// Get Masseges Method...
void messegesStream() async {
await for (var snapshot in _fireStore.collection('messeges').snapshots()) {
for (var message in snapshot.docs) {
print(message.data());
}
}
}
//------------------------------------------------------------------------------
// Code Start...
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.deepPurpleAccent,
leading: null,
actions: <Widget>[
IconButton(
icon: const Icon(Icons.download),
onPressed: () {
messegesStream();
// _auth.signOut();
// Navigator.pop(context);
}),
],
title: const Text('⚡️Chat'),
),
body: Container(
constraints: const BoxConstraints.expand(),
decoration: const BoxDecoration(
image: DecorationImage(
image: AssetImage("images/background.jpg"), fit: BoxFit.cover),
),
child: SafeArea(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
//------------------------------------------------------------
// StreamBuilder method...
StreamBuilder<QuerySnapshot>(
stream: _fireStore.collection('messages').snapshots(),
builder: (context, snapshot) {
List<Text> messageWidgets = [];
if (snapshot.hasData) {
final messages = snapshot.data!.docs;
for (var message in messages) {
final messageText = message.get('text');
final messageSender = message.get('sender');
final messageWidget =
Text('$messageSender said $messageText');
messageWidgets.add(messageWidget);
}
}
return Column(
children: messageWidgets,
);
},
),
//------------------------------------------------------------
Container(
decoration: kMessageContainerDecoration,
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Expanded(
child: TextField(
onChanged: (value) {
messegeText = value;
},
decoration: kMessageTextFieldDecoration,
),
),
MaterialButton(
onPressed: () {
_fireStore.collection('messeges').add(
{
'text': messegeText,
'sender': loogedInUser.email,
},
);
},
child: const Text(
'Send',
style: kSendButtonTextStyle,
),
),
],
),
),
],
),
),
),
);
}
}
my android emulator screen,
Solution 1:[1]
Try this code:
StreamBuilder<QuerySnapshot>(
stream: _fireStore.collection('messages').snapshots(),
builder: (BuildContext context,
AsyncSnapshot<QuerySnapshot> chatSnapshot) {
if (chatSnapshot.connectionState == ConnectionState.waiting) {
return Center(
child: Container(),
);
}
return ListView(
reverse: true,
controller: _controller,
physics: const BouncingScrollPhysics(),
children:
chatSnapshot.data!.docs.map((DocumentSnapshot document) {
Map<String, dynamic> data =
document.data()! as Map<String, dynamic>;
return Text(data["text"]);
}).toList(),
);
},
),
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Saitoh Akira |

