我在dart中使用代码获取api数据。 代码没有显示任何错误,也没有响应数据。
API链接:https://jsonplaceholder.typicode.com/users
环境:
sdk: ">=2.1.0 <3.0.0"
依赖性: 扑: SDK:颤振 http:任何
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
,然后将其添加到我的代码中,并且不会显示任何数据。