【发布时间】:2021-08-24 19:54:46
【问题描述】:
我看了这个教程,但我卡在返回未来列表 https://www.youtube.com/watch?v=lTKFYGHx16M 试图连接 mysql php api 和颤振代码
import '../model/usersdata.dart';
import 'package:http/http.dart' as http;
Future<List<Userprofile>> fetchUsers() async {
var url = Uri.parse("http://192.168.100.68/mlm_sys/api_request/get_data.php");
final response = await http.get(url);
print(response.body);
return userprofileFromJson(response.body);<----ERROR LINE
ERROR: //'Userprofile' 类型的值
无法从函数“fetchUsers”返回
因为它的返回类型为“Future”。
}
///=====MODEL
import 'dart:convert';
Userprofile userprofileFromJson(String str) =>
Userprofile.fromJson(json.decode(str));
String userprofileToJson(Userprofile data) => json.encode(data.toJson());
class Userprofile {
Userprofile({
required this.id,
required this.sponsor,
required this.fname,
required this.lname,
required this.mname,
required this.pnum,
required this.address,
required this.email,
required this.username,
required this.password,
required this.dateRegistration,
});
........AND SO ON
【问题讨论】:
-
添加
fetchUsers()函数的使用方式 -
Userprofile 不是 Userprofile 列表...您需要映射您的响应并变成列表
-
@NirmalCode 我稍后会在我的小部件中使用未来的构建器
-
@MarianoZorrilla 我的模型中有地图这是你的意思吗? Map
toJson() => { “id”:id,“sponsor”:赞助商,“fname”:fname,“lname”:lname,“mname”:mname,“pnum”:pnum,“地址": 地址, "email": 电子邮件, "username": 用户名, "password": 密码, "date_registration": "${dateRegistration.year.toString().padLeft(4, '0')}-${dateRegistration .month.toString().padLeft(2, '0')}-${dateRegistration.day.toString().padLeft(2, '0')}", };
标签: flutter dart asynchronous