gpt4 book ai didi

flutter - 如何在Flutter中使用自定义模型从Future获得值(value)

转载 作者:行者123 更新时间:2023-12-03 03:24:32 25 4
gpt4 key购买 nike

我正在调用API以从服务器获取数据,并且已经从(https://javiercbk.github.io/json_to_dart/)创建了一个dart文件(模型)

现在,我想访问该将来的对象值。

主镖

@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Row(
children: <Widget>[
RaisedButton(
child: Text("Click"),
onPressed: () async{
setState(() {
apiCall = true; // Set state like this
});
MemberLogin fMain = await getUser();
print('$fMain ');
},
),
],
),
),
);
}

Future<MemberLogin> getUser() async {
try {
final String _endpoint =
"https://api.com/";
Dio dio = new Dio();
Response response = await dio
.post(_endpoint, data: {"new_data": "hello"});
print("user API response - : $response ");
setState(() {
apiCall = false;
});
return MemberLogin.fromJson(response.data);
} catch (error, stacktrace) {
print("Exception occured: $error stackTrace: $stacktrace");
//return MemberLogin.withError("$error");
}
}

MemberLogin.dart
class MemberLogin {
int success;
String message;

MemberLogin({this.success, this.message});

MemberLogin.fromJson(Map<String, dynamic> json) {
success = json['success'];
message = json['message'];
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['success'] = this.success;
data['message'] = this.message;
return data;
}
}

现在,当我在请求 message之后从 MemberLogin打印 MemberLogin fMain = await getUser();时。
我调试了代码,可以看到响应,但无法打印或访问消息字符串。

我怎样才能做到这一点 ?

最佳答案

@deepak,我模拟了一个api,似乎工作正常。您是否尝试以fMain.message的身份访问消息?请参见下面的示例,

class MyAppState extends State<MyApp> {
bool apiCall = false;
String message = '';

@override
Widget build(BuildContext context) {
return MaterialApp(home: Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(message, style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18.0),),
RaisedButton(
child: Text("Click", style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18.0),),
onPressed: () async{
apiCall = true; // Set state like this
MemberLogin fMain = await getUser();
message = fMain.message;
setState(() {
});
print('$fMain ');
},
),
],
),
),
));
}

Future<MemberLogin> getUser() async {
try {
final String _endpoint =
"http://echo.jsontest.com/key/value/message/testmessage";
Dio dio = new Dio();
Response response = await dio
.get(_endpoint);
print("user API response - : $response ");
setState(() {
apiCall = false;
});
return MemberLogin.fromJson(response.data);
} catch (error, stacktrace) {
print("Exception occured: $error stackTrace: $stacktrace");
//return MemberLogin.withError("$error");
}
}
}

class MemberLogin {
String key;
String message;

MemberLogin({this.key, this.message});

MemberLogin.fromJson(Map<String, dynamic> json) {
key = json['key'];
message = json['message'];
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['key'] = this.key;
data['message'] = this.message;
return data;
}
}


demo

关于flutter - 如何在Flutter中使用自定义模型从Future获得值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59288538/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com