首页 > 解决方案 > 根据来自 URL "[FLUTTER] " 的 JSON 值进行重定向

问题描述

该 URL 具有 JSON 格式的返回值。这里的数据以 [{"operation":"0"}] 或 [{"operation":"1"}] 的形式出现。根据这里的值,我会在flutter中重定向到另一个站点。我无法解决问题。我试过的代码如下。

Future<List> login() async{
final response = await http.get("https://www.exampleurl.com");

var datauser = response.body;

if(datauser == 0 ){

print(datauser);
print(Text("Giriş Başarılı"),
);
}
else{
print(datauser);
print(Text(“Giriş Başarısız”));
}
}

我把它作为这段代码的输出。

颤振:[{“islem”:0}] 颤振:文本(“Giriş Başarısız”)

标签: jsonflutterdart-html

解决方案


You should use jsonDecode to access operation value

Future<List> login() async{
final response = await http.get("https://www.exampleurl.com");

var datauser = jsonDecode(response.body)[0];

if(datauser['operation'] == 0 ){

print(datauser);
print(Text("Giriş Başarılı"),
);
}
else{
print(datauser);
print(Text(“Giriş Başarısız”));
}
}

do not forget to import

import 'dart:convert';

推荐阅读