聊天室服务
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:onpods/constants/http_error_handelling.dart';
import 'package:onpods/constants/utils.dart';
import 'package:onpods/models/chat_room_model.dart';
import 'package:onpods/models/user_model.dart';
import 'package:http/http.dart' as http;
import 'package:onpods/providers/user_provider.dart';
import 'package:onpods/view/chat_room.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:provider/provider.dart';
import '../../constants/global_constants.dart';
class ChatRoomServices {
void getChatRoomIndex({
required BuildContext context,
}) async {
http.Response res = await http.get(
Uri.parse("$uri/get-all-room-index"),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
);
print("skjnv");
print(jsonDecode(res.body)[0]);
List<ChatRoomModel> allChatRooms = [];
for (var e in jsonDecode(res.body)) {
ChatRoomModel temp = ChatRoomModel.fromJson(e.toString());
allChatRooms.add(temp);
print(temp.chatTopic);
print(temp.chatTopic);
}
print("skjnv");
ChatRoom chatRoom;
// ignore: use_build_context_synchronously
httpErrorHandle(
response: res,
context: context,
onSuccess: () async {},
);
}
}
聊天室模型
import 'dart:convert';
// ignore_for_file: public_member_api_docs, sort_constructors_first
class ChatRoomModel {
String chatTopic;
String roomType;
Map<String, dynamic> ownerId;
List<Map<String, dynamic>> speakers;
List<Map<String, dynamic>> participants;
String createdAt;
String updatedAt;
ChatRoomModel({
required this.chatTopic,
required this.roomType,
required this.ownerId,
required this.speakers,
required this.participants,
required this.createdAt,
required this.updatedAt,
});
Map<String, dynamic> toMap() {
return <String, dynamic>{
'chatTopic': chatTopic,
'roomType': roomType,
'ownerId': ownerId,
'speakers': speakers,
'participants': participants,
'createdAt': createdAt,
'updatedAt': updatedAt,
};
}
factory ChatRoomModel.fromMap(Map<String, dynamic> map) {
return ChatRoomModel(
chatTopic: map['chatTopic'] as String,
roomType: map['roomType'] as String,
ownerId: Map<String, dynamic>.from((map['ownerId'] as Map<String, dynamic>)),
speakers: List<Map<String, dynamic>>.from(
(map['speakers'] as List<int>).map<Map<String, dynamic>>(
(x) => x as Map<String, dynamic>,
),
),
participants: List<Map<String, dynamic>>.from(
(map['participants'] as List<int>).map<Map<String, dynamic>>(
(x) => x as Map<String, dynamic>,
),
),
createdAt: map['createdAt'] as String,
updatedAt: map['updatedAt'] as String,
);
}
String toJson() => json.encode(toMap());
factory ChatRoomModel.fromJson(String source) {
print("Sajs");
print(source);
return ChatRoomModel.fromMap(json.decode(source) as Map<String, dynamic>);
}
}
Postman请求响应(/get-all-room-index)
[
{
"_id": "6414a040c01cb2fbea9ede6c",
"chatTopic": "Current Affairs",
"roomType": "public",
"ownerId": {
"isPublic": true,
"_id": "6405fe3df459de43b5dadb78",
"user_name": "Simran Bhalode",
"user_email": "simran@gmail.com",
"user_password": "$2b$07$VPHjLNYKicBcEgfyRNT9i.8zok9kU7XqNlNgnpAsZ5V9FN/oyKUka",
"user_role": "admin",
"registered_date": "2023-03-06T14:52:45.847Z",
"__v": 0
},
"speakers": [
{
"isPublic": true,
"_id": "6405fe3df459de43b5dadb78",
"user_name": "Simran Bhalode",
"user_email": "simran@gmail.com",
"user_password": "$2b$07$VPHjLNYKicBcEgfyRNT9i.8zok9kU7XqNlNgnpAsZ5V9FN/oyKUka",
"user_role": "admin",
"registered_date": "2023-03-06T14:52:45.847Z",
"__v": 0
}
],
"participants": [],
"createdAt": "2023-03-17T17:15:44.873Z",
"updatedAt": "2023-03-17T17:15:44.873Z",
"__v": 0
},
{
"_id": "6415e11ddd95481799b016f7",
"chatTopic": "Current Affairs",
"roomType": "public",
"ownerId": {
"isPublic": true,
"_id": "6405fe3df459de43b5dadb78",
"user_name": "Simran Bhalode",
"user_email": "simran@gmail.com",
"user_password": "$2b$07$VPHjLNYKicBcEgfyRNT9i.8zok9kU7XqNlNgnpAsZ5V9FN/oyKUka",
"user_role": "admin",
"registered_date": "2023-03-06T14:52:45.847Z",
"__v": 0
},
"speakers": [
{
"isPublic": true,
"_id": "6405fe3df459de43b5dadb78",
"user_name": "Simran Bhalode",
"user_email": "simran@gmail.com",
"user_password": "$2b$07$VPHjLNYKicBcEgfyRNT9i.8zok9kU7XqNlNgnpAsZ5V9FN/oyKUka",
"user_role": "admin",
"registered_date": "2023-03-06T14:52:45.847Z",
"__v": 0
}
],
"participants": [],
"createdAt": "2023-03-18T16:04:45.928Z",
"updatedAt": "2023-03-18T16:04:45.928Z",
"__v": 0
}
]
错误为
[VERBOSE-2:dart_vm_initializer.cc(41)] Unhandled Exception: FormatException: Unexpected character (at character 2)
{_id: 6414a040c01cb2fbea9ede6c, chatTopic: Current Affairs, roomType: publi...
^
#0 _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1383:5)
#1 _ChunkedJsonParser.parse (dart:convert-patch/convert_patch.dart:913:48)
#2 _parseJson (dart:convert-patch/convert_patch.dart:35:10)
#3 JsonDecoder.convert (dart:convert/json.dart:610:36)
#4 JsonCodec.decode (dart:convert/json.dart:216:41)
#5 new ChatRoomModel.fromJson
chat_room_model.dart:59
#6 ChatRoomServices.getChatRoomIndex
chat_room_services.dart:33
<asynchronous suspension>
*
Postman 成绩
我试着改变转换去数据类型,但没有工作,任何人都可以请建议我一个更好的解决方案,这是显示错误的_id,但为什么我不知道。
[
{
"_id": "6414a040c01cb2fbea9ede6c",
"chatTopic": "Current Affairs",
"roomType": "public",
"ownerId": {
"isPublic": true,
"_id": "6405fe3df459de43b5dadb78",
"user_name": "Simran Bhalode",
"user_email": "simran@gmail.com",
"user_password": "$2b$07$VPHjLNYKicBcEgfyRNT9i.8zok9kU7XqNlNgnpAsZ5V9FN/oyKUka",
"user_role": "admin",
"registered_date": "2023-03-06T14:52:45.847Z",
"__v": 0
},
"speakers": [
{
"isPublic": true,
"_id": "6405fe3df459de43b5dadb78",
"user_name": "Simran Bhalode",
"user_email": "simran@gmail.com",
"user_password": "$2b$07$VPHjLNYKicBcEgfyRNT9i.8zok9kU7XqNlNgnpAsZ5V9FN/oyKUka",
"user_role": "admin",
"registered_date": "2023-03-06T14:52:45.847Z",
"__v": 0
}
],
"participants": [],
"createdAt": "2023-03-17T17:15:44.873Z",
"updatedAt": "2023-03-17T17:15:44.873Z",
"__v": 0
},
{
"_id": "6415e11ddd95481799b016f7",
"chatTopic": "Current Affairs",
"roomType": "public",
"ownerId": {
"isPublic": true,
"_id": "6405fe3df459de43b5dadb78",
"user_name": "Simran Bhalode",
"user_email": "simran@gmail.com",
"user_password": "$2b$07$VPHjLNYKicBcEgfyRNT9i.8zok9kU7XqNlNgnpAsZ5V9FN/oyKUka",
"user_role": "admin",
"registered_date": "2023-03-06T14:52:45.847Z",
"__v": 0
},
"speakers": [
{
"isPublic": true,
"_id": "6405fe3df459de43b5dadb78",
"user_name": "Simran Bhalode",
"user_email": "simran@gmail.com",
"user_password": "$2b$07$VPHjLNYKicBcEgfyRNT9i.8zok9kU7XqNlNgnpAsZ5V9FN/oyKUka",
"user_role": "admin",
"registered_date": "2023-03-06T14:52:45.847Z",
"__v": 0
}
],
"participants": [],
"createdAt": "2023-03-18T16:04:45.928Z",
"updatedAt": "2023-03-18T16:04:45.928Z",
"__v": 0
}
]
1条答案
按热度按时间oprakyz71#
这是你的json到dart文件id部分的格式错误,像这样使用