Home > Mobile >  I cannot stream messages from firebase in flutter
I cannot stream messages from firebase in flutter

Time:04-06

I am learning Flutter chat app from Angela YU but this part of code is just like as she wrote but for me not working and it has lots of errors

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;
            List<Text> messageWidgets = [];
            for (var message in messages!) {
              final messageText = message.data['text'];
              final messageSender = message.data['sender'];
              final messageWidget =
                  Text('$messageText from $messageSender');
              messageWidget.add(messageWidget);
            }
            return Column(
              children: [
                messageWidgets,
              ],
            );
          },
        ),

here is the errors :

lib/screens/chat_screen.dart:72:53: Error: The operator '[]' isn't defined for the class 'Object? Function()'.
  • 'Object' is from 'dart:core'. Try correcting the operator to an existing operator, or defining a '[]' operator. final messageText = message.data['text']; ^ lib/screens/chat_screen.dart:73:55: Error: The operator '[]' isn't defined for the class 'Object? Function()'.
  • 'Object' is from 'dart:core'. Try correcting the operator to an existing operator, or defining a '[]' operator. final messageSender = message.data['sender']; ^ lib/screens/chat_screen.dart:76:35: Error: The method 'add' isn't defined for the class 'Text'.
  • 'Text' is from 'package:flutter/src/widgets/text.dart' ('/C:/src/flutter/packages/flutter/lib/src/widgets/text.dart'). Try correcting the name to the name of an existing method, or defining a method named 'add'. messageWidget.add(messageWidget); ^^^ lib/screens/chat_screen.dart:80:23: Error: A value of type 'List' can't be assigned to a variable of type 'Widget'.
  • 'List' is from 'dart:core'.
  • 'Text' is from 'package:flutter/src/widgets/text.dart' ('/C:/src/flutter/packages/flutter/lib/src/widgets/text.dart').
  • 'Widget' is from 'package:flutter/src/widgets/framework.dart' ('/C:/src/flutter/packages/flutter/lib/src/widgets/framework.dart'). messageWidgets, ^ lib/screens/chat_screen.dart:67:24: Error: A non-null value must be returned since the return type 'Widget' doesn't allow null.
  • 'Widget' is from 'package:flutter/src/widgets/framework.dart' ('/C:/src/flutter/packages/flutter/lib/src/widgets/framework.dart'). builder: (context, snapshot) { ^

CodePudding user response:

Change your code like this

            final messages = snapshot.data?.docs;
            List<Text> messageWidgets = [];
            for (var message in messages) {
              var data = message.data() as Map; // add Typecast 
              final messageText = data['text'];
              final messageSender = data['sender'];
              final messageWidget = Text('$messageText from $messageSender');
              messageWidgets.add(messageWidget); // you have to add item to list
            }
            return Column(
              children: messageWidgets // your list should assign to children
            );

CodePudding user response:

The error is coming up as a result of breaking changes in the version upgrades to the Firestore library.

See https://firebase.flutter.dev/docs/firestore/2.0.0_migration/

Solution:

Change this

final messageText = message.data['text'];

to this:

final messageText = (message.data as Map<String, dynamic>)['text'];
  • Related