错误:应使用类型为"字符串"的值,但获得类型为"_MapStream<动态,可迭代<Post>>"的值



我在服务器上写了以下代码。

服务器代码:

import 'dart:convert';
import 'package:mongo_dart/mongo_dart.dart';
import 'package:shelf/shelf.dart';
import 'package:shelf_web_socket/shelf_web_socket.dart';
import 'package:web_socket_channel/web_socket_channel.dart';
class PostsSocketApi {
PostsSocketApi(this.store);
final List<WebSocketChannel> _sockets = [];
final DbCollection store;
Handler get router {
return webSocketHandler((WebSocketChannel socket) {
socket.stream.listen((message) async {
final data = json.decode(message);
print(data);
if (data['action'] == 'LOGIN') {
final user = await store.findOne(
where.eq('name', data['name']).eq('password', data['password']));
if (user != null) {
for (final ws in _sockets) {
ws.sink.add(json.encode('name'));
// probably there must be .toString()       ^^^             
}
}
if (user == null) {
for (final ws in _sockets) {
ws.sink.addError('NOSUCHUSER');
}
}
}
});
_sockets.add(socket);
});
}
}

现在,我想将"name"字段放入变量tmp_name中,将其与登录字段中的name进行比较,如下所示:

登录代码:

void loginUser() async {
final name = emailController.text;
final password = passwordController.text;
widget.api.send(
json.encode({'action': 'LOGIN', 'name': name, 'password': password}));
String tmp_name = widget.api.getName;
// method from API               ^^^^^^^
if (tmp_name == name) {
SharedPreferences prefs = await SharedPreferences.getInstance();
setState(() {
prefs.setString('name', name);
});
Navigator.of(context)
.pushReplacement(MaterialPageRoute(builder: (context) => Home()));
} else {
print('error: no such user');
}
}

应用程序中的API代码:

class PostsSocketApi {
PostsSocketApi()
: _api = WebSocketChannel.connect(
Uri.parse('ws://localhost:8082/posts-ws/'));
final WebSocketChannel _api;
Stream<List<Post>> get stream => _api.stream.map<List<Post>>((data) {
final decoded = json.decode(data);
return (decoded as List)
.map<Post>(
(json) => Post.fromJson(json),
)
.toList();
});
ValueChanged<String> get send => _api.sink.add;
get getName => _api.stream.map((data) {
final decoded = json.decode(data['name']);
return (decoded as List).map<Post>(
(json) => Post.fromJson(json),
);
});
}

然而,我在APP代码中出现了以下错误,不知道如何解决。

Error: Expected a value of type 'String', but got one of type '_MapStream<dynamic, Iterable<Post>>'

服务器代码工作正常(比较数据并打印,如果是错误的用户(

注册服务器响应:

{action: ADDUSER, name: 6, password: 6, id: 6}

服务器以现有用户身份登录时的响应:

{action: LOGIN, name: 6, password: 6}

服务器以非现有用户身份登录时的响应:

{action: LOGIN, name: fqfqfq, password: qfqfqfq}
NOSUCHUSER
ERROR - 2022-03-21 11:55:27.183011
Asynchronous error
type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String'
PostsSocketApi.router.<fn>.<fn>
package:io/api/socket_api.dart:79

对于Ruchit:

userID类型为String。如果我这样写,就会抛出同样的错误。

get getName => _api.stream.map((data) {
final decoded = json.decode(data['name']);
return (decoded as List)
.map<String>((json) => Post.fromJson(json).userID);
});

但是,如果我这样写,我会得到类型错误:错误:返回类型"String"不是闭包上下文所要求的"Post">

代码:

get getName => _api.stream.map((data) {
final decoded = json.decode(data['name']);
return (decoded as List)
.map<Post>((json) => Post.fromJson(json).userID );
});

有什么想法吗?(甚至关于重构其他代码(

我试图在文档中找到一些解释,但是。。。如果你需要更多的代码或解释,请写下来。或者,如果你能给我关于如何通过另一种方式登录的建议,请告诉我。

如果可以的话,请帮帮我<3

这里返回的是整个post对象,而得到的是不匹配的String,因此显示错误。

get getName => _api.stream.map((data) {
final decoded = json.decode(data['name']);
return (decoded as List).map<Post>(
(json) => Post.fromJson(json).field,  //here field is the field you want from Post object and you are getting string so this field should have type string.
//eg. Post.fromJson(json).name
);
});

相关内容

最新更新