【问题标题】:How to display variable from json return in text如何以文本形式显示来自json返回的变量
【发布时间】:2019-09-01 16:09:22
【问题描述】:
String empName;
Future<List> getUserData() async{
  final response = await http.post("http://172.16.161.34:8080/ebs/cfs/android_test_app/accessfile.php?q=getUserData",body:{
    "emp_id": widget.empId,
  });
  var dataUser = jsonDecode(response.body);
  empName = dataUser[0]['name'];

  return null;
}

如何在第 2 行到第 70 行“child: Text('')”中显示变量“empName”

Full code on Pastebin

【问题讨论】:

    标签: dart flutter


    【解决方案1】:

    尝试这种方式..为这样的响应数据制作 pojo 类..

     class UserData {
     final int albumId;
     final int id;
     final String title;
     final String url;
     final String thumbnailUrl;
    
     UserData({this.albumId, this.id, this.title, this.url, this.thumbnailUrl});
    
     factory UserData.fromJson(Map<String, dynamic> json) {
      return new UserData(
        albumId: json['albumId'],
        id: json['id'],
        title: json['title'],
        url: json['url'],
        thumbnailUrl: json['thumbnailUrl']);
     }
     }
    

    制作api调用方法..

    Future<UserData> fetchData() async {
     var result = await get('https://jsonplaceholder.typicode.com/photos');
    
     if (result.statusCode == 200) {
      return UserData.fromJson(json.decode(result.body));
     } else {
    // If that response was not OK, throw an error.
    throw Exception('Failed to load post');
    }
    }
    

    然后创建获取数据的全局对象..

      Future<UserData> userDataList;
    

    按钮点击..

                userDataList = fetchData();
    

    之后你想打印数据..

    userDataList.then((userData){
      print(userData.title);
    });
    

    【讨论】:

      【解决方案2】:

      首先,getUserData() 函数永远不会返回任何内容。似乎您只需要名称,因此此函数可能如下所示:

      Future<String> getUserData() async{
        final response = await http.post("http://172.16.161.34:8080/ebs/cfs/android_test_app/accessfile.php?q=getUserData",body:{
          "emp_id": widget.empId,
        });
        var dataUser = jsonDecode(response.body);
        return dataUser[0]['name'];
      }
      

      然后要设置empName 变量,您应该使用setState()。 因此,将您的 afterFirstLayout() 方法更改为:

      @override
      void afterFirstLayout(BuildContext context) async {
        // Calling the same function "after layout" to resolve the issue.
        getUserData().then( (userName) {
          setState(() {
            empName = userName;
          });
        });
      }
      

      您似乎还想在按下IconButton 后重新加载名称。 所以你可能想用这个覆盖你的代码:

      IconButton(icon: Icon(Icons.shopping_cart),
        onPressed:() {
          getUserData().then( (userName) {
            setState(() {
            empName = userName;
            });
          });
        },
      ),
      

      【讨论】:

      • setState(() { empName = getUserData(); });是一个错误
      • 我再次更新了我的答案。还修改了您的 IconButton 代码。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-24
      • 1970-01-01
      • 2017-05-03
      相关资源
      最近更新 更多