我在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
然后将其添加到我的代码,它不会'显示任何数据。