Home > Software design >  Flutter does not output all the results during a post request
Flutter does not output all the results during a post request

Time:08-30

Flutter does not output all the results during a post request. Out of about 260 comes to the list, 113 are saved.

...............................................................................................................................................................................................................

Future<List<NewChatModel>> getAllChats({@required String userId}) async {
  final response = await http.post(
    Uri.parse(URL),
    headers: <String, String>{
      'Content-Type': 'application/json; charset=UTF-8',
    },
    body: jsonEncode(<String, int>{
      'first_user_id': int.parse(userId),
    }),
  );
  if (response.statusCode == 200) {
    List<NewChatModel> returnList = [];
    for (var i in jsonDecode(response.body)) {
      returnList.add(NewChatModel.fromJson(i));
    }
    print(returnList.length);
    return returnList;
  } else {
    return null;
  }
}

    class NewChatModel {
  String id;
  String chatId;
  String messageId;
  String message;
  String messageDate;
  String schoolId;
  String fullName;
  String phone;
  String email;
  String urlProfileImage;
  String birthday;
  String roleId;
  String lastActivity;
  String isOnline;

  NewChatModel(
      {this.id,
      this.chatId,
      this.messageId,
      this.message,
      this.messageDate,
      this.schoolId,
      this.fullName,
      this.phone,
      this.email,
      this.urlProfileImage,
      this.birthday,
      this.roleId,
      this.lastActivity,
      this.isOnline});

  factory NewChatModel.fromJson(dynamic json) {
    return NewChatModel(
      id: json['id'].toString(),
      chatId: json['chat_id'].toString(),
      messageId: json['message_id'].toString(),
      message: json['message'].toString(),
      messageDate: json['message_date'].toString(),
      schoolId: json['school_id'].toString(),
      fullName: json['full_name'].toString(),
      phone: json['phone'].toString(),
      email: json['email'].toString(),
      urlProfileImage: json['urlProfileImage'].toString(),
      birthday: json['birthday'].toString(),
      roleId: json['role_id'].toString(),
      lastActivity: json['last_activity'].toString(),
      isOnline: json['is_online'].toString(),
    );
  }
}

Edit: added NewChatModel code But I don`t think that its help solve problem I think problem in String limit, idk

CodePudding user response:

If at all it helps you, I have a list of chat conversations in my app, too. I collect them with a Stream and display them with a StreamBuilder(). This is a very resource efficient way to do it, without the need to actually collect all the conversations at once! The StreamBuilder() widget makes sure it collects only the conversations that are currently visible on the screen (plus some).

This is what it looks like:

import 'package:my_giggz/firebase_labels.dart';
import 'package:my_giggz/my_firebase.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';

class MyMessagesScreen extends StatefulWidget {
  @override
  _MyMessagesScreenState createState() {
    return _MyMessagesScreenState();
  }
}

class _MyMessagesScreenState extends State<MyMessagesScreen> {
  String myUid = MyFirebase.authObject.currentUser!.uid;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: StreamBuilder<QuerySnapshot>(
        // Stream of all the conversations in the database that contain my uid:
        stream: MyFirebase.storeObject.collection(kCollectionConversations).where(kFieldParticipantsArray, arrayContains: myUid).orderBy(kFieldLastTimeStamp, descending: true).snapshots(),
        builder: (context, asyncSnapshot) {
          List<Widget> convCards = [];
          QuerySnapshot? foundConversations = asyncSnapshot.data;
          if (foundConversations != null) {
            //It always wants to be null at first, and then I get errors for calling on null.
            for (QueryDocumentSnapshot conv in foundConversations.docs) {
              Map<String, dynamic> convData = conv.data() as Map<String, dynamic>;
              convCards.add(
                ConvCard(convData)  // A homemade widget that takes a Map argument to display some data from the conversation
              );
              // i  ;
            }
          } else {
            // For as long as the found conversations are null, a spinner will be shown:
            return Center(child: CircularProgressIndicator());
          }
          // This part will only be reached if conversations were found:
          return ListView.builder(
            padding: EdgeInsets.all(0),
            itemCount: convCards.length,
            itemBuilder: (context, index) {
              return convCards[index];
            },
          );
        },
      ),
    );
  }
}

If you have any questions on that, I'm happy to answer.

CodePudding user response:

I do not know why this is so, but here is the answer to my question :)

For some reason, he doesn't want to show the entire length of the list, but he filled it out absolutely correctly)

  • Related