在 Flutter 中描绘未来?
Mapping a future in flutter?
我正在尝试使用 Dio flutter 包进行一些响应处理
我有以下功能
Future<Map<String, dynamic>> forgotPassword(Map body) => client.dio
.post('/user/forgotpassword', data: body)
.then(
(response) => {
print('POST /user/forgotpassword $response'),
response.data as Map<String, dynamic>
},
onError: (error) => {
throw error
})
.catchError((error) => client.handleRequestError(error));
我预计这是 return 一个 Future<Map<String, dynamic>>
,我在 response.data as Map<String, dynamic>
上指定,但我从编译器中收到 dart(return_of_invalid_type)
错误。
A value of type 'Future<Set<void>> can't be returned from method 'forgotPassword' because it has a return type of 'Future<Map<String, dynamic>>'.
return 将此 Future 的值映射到映射(或任何其他 class,就此而言)的正确方法是什么?
您 .then
的回调中有一些奇怪的语法。如果你放弃 =>
符号,事情应该有效:
Future<Map<String, dynamic>> forgotPassword(Map body) => client.dio
.post('/user/forgotpassword', data: body)
.then(
(response) {
print('POST /user/forgotpassword $response'),
response.data as Map<String, dynamic>
},
onError: (error) => {
throw error
})
.catchError((error) => client.handleRequestError(error));
仅对单行语句使用 =>
,因为它 return 是语句的结果。您看到 return 类型的 Future<Set<void>>
的原因是因为您正在通过执行 (response) => { /* this is treated as a Set */ }
创建 Set
我正在尝试使用 Dio flutter 包进行一些响应处理
我有以下功能
Future<Map<String, dynamic>> forgotPassword(Map body) => client.dio
.post('/user/forgotpassword', data: body)
.then(
(response) => {
print('POST /user/forgotpassword $response'),
response.data as Map<String, dynamic>
},
onError: (error) => {
throw error
})
.catchError((error) => client.handleRequestError(error));
我预计这是 return 一个 Future<Map<String, dynamic>>
,我在 response.data as Map<String, dynamic>
上指定,但我从编译器中收到 dart(return_of_invalid_type)
错误。
A value of type 'Future<Set<void>> can't be returned from method 'forgotPassword' because it has a return type of 'Future<Map<String, dynamic>>'.
return 将此 Future 的值映射到映射(或任何其他 class,就此而言)的正确方法是什么?
您 .then
的回调中有一些奇怪的语法。如果你放弃 =>
符号,事情应该有效:
Future<Map<String, dynamic>> forgotPassword(Map body) => client.dio
.post('/user/forgotpassword', data: body)
.then(
(response) {
print('POST /user/forgotpassword $response'),
response.data as Map<String, dynamic>
},
onError: (error) => {
throw error
})
.catchError((error) => client.handleRequestError(error));
仅对单行语句使用 =>
,因为它 return 是语句的结果。您看到 return 类型的 Future<Set<void>>
的原因是因为您正在通过执行 (response) => { /* this is treated as a Set */ }
Set