'streambuilder I got the snap but can not transfer to list firebase database rtdb flutter [duplicate]

It almost kills me for today T T, I got the data but can not successfully convert to the List I can use, I got help from Frank from this link Get Map<String, dynamic> from Map<dynamic, dynamic> flutter

But still I got null, please check my code thanks:

StreamBuilder(
                stream: messageStream.onValue,
                builder: (context, snap) {
                  if (snap.hasData &&
                      !snap.hasError &&
                      snap.data.snapshot.value != null) {

print('snapshot value${snap.data.snapshot.value.toString()}'); 
// but here I can see the data from snap
//I/flutter ( 6189): snapshot value{-My6relBpWvPaY_I4JvN: {idUser: 4dca8440-a37d-11ec-9c66-9b8f61be17f0, message: 777777}}


                    DataSnapshot snapshot = snap.data.snapshot;
                    final data = Map<String, dynamic>.from(snapshot.value as Map);
                    Message message = Message.fromJson(data);

                    print('message value${message.message}'); // here I got null
                    List<Message> messages = [];
                    messages.add(message);


                    return ListView.builder(
                      reverse: true,
                      shrinkWrap: true,
                      controller: _scrollController,
                      itemCount: (messages == null) ? 0 : messages.length,
                      itemBuilder: (itemBuilderContext, index) {
                        final message = messages[index];
                        return MessageWidget(
                          message: message,
                          isMe: message.userId == currentUserId,
                        );
                      },
                    );
                  } else {
                    return const Center(child: CircularProgressIndicator());
                  }
                }), 

Message model is very simple:

class Message {
  final String userId;
  final String message;

  const Message({
    @required this.userId,
    @required this.message,
  });

  static Message fromJson(Map<String, dynamic> json) => Message(
        userId: json['idUser'],
        message: json['message'],
      );

  Map<String, dynamic> toJson() => {
        'idUser': userId,
        'message': message,
      };
}


Solution 1:[1]

hopefully it is the right solution and inspirational for the beginner like me,

finally I got the data from stream by using Map<String,dynamic> twice for avoding the convert error () please check code below:

            DataSnapshot snapshot = snap.data.snapshot;

            print(
                'snapshot${snap.data.snapshot.value.toString()}');
            List<Message> messages = [];
            final Map<String,dynamic> data = Map<String, dynamic>.from(
                snapshot.value as Map);

            for (var element in data.values) {
              final Map<String,dynamic> messageData = Map<String, dynamic>.from(
                  element as Map);
              Message message = Message.fromJson(messageData);
              messages.add(message);
              print('message?${message.message}');

thank Frank as always for providing the solution and guidance!

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 xo1000