用dart获取json数据的问题

问题描述 投票:0回答:1

我在dart中使用代码来获取api数据。代码没有显示任何错误,也没有响应数据。

API链接:https://jsonplaceholder.typicode.com/users

环境:sdk: ">=2.1.0 <3.0.0"

依赖:flutter:sdk:flutter http:any

import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

void main() async {
    List _data =  await getJSON();
    print("hello world ");
    runApp(
    new MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text("Jason Pddarsin"),
          backgroundColor: Colors.orange,
          centerTitle: true,
        ),

        body: ListView.builder(
            itemCount: _data.length,
            itemBuilder: (BuildContext context, int pos) {
              return Column(
                children: <Widget>[
                  Divider(
                    height: 5.5,
                  ),
                  Text(_data[pos]["name"]),
                ],
              );
            }),
      ),
     ),
    );
    }

    Future<List> getJSON() async {
    String apiUrl = "https://jsonplaceholder.typicode.com/users";
    http.Response response = await http.get(apiUrl);
    return json.decode(response.body);
}    

如果我直接使用该代码移动获取白屏,我使用一个简单的应用程序运行materialapp然后将其添加到我的代码,它不会'显示任何数据。

json api http dart flutter
1个回答
0
投票

我只是将你的代码复制并粘贴在一个全新的扑动项目上,它运行良好。你有任何错误信息吗?你说你把它放在另一个MaterialApp里面?如果是这种情况,请尝试直接从主设备调用此设备,而不是在MaterialApp内部。 enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.