我有用于获取列表的 auth_key。
我尝试了很多次,但 auth_key 没有进入第二个屏幕,因为我无法从 Internet 获取数据我在 Postman 中使用 JSON 请求标头测试了 API,它似乎工作正常。
这是响应... { "error": false, "message": "success", "course_list": [ { "courseId": "19", "courseName": "Verbs-01-def-prctice", “价格”:“0”,“sub_type”:“1”,“sku_id”:“15464”,“courseIcon”:“”,“类型”:“0”,“付费”:假},
**Now on my Flutter application I am trying to do the same thing:
**
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'package:http/http.dart';
class SecondScreen extends StatefulWidget {
@override
_SecondScreenState createState() => _SecondScreenState();
}
class _SecondScreenState extends State<SecondScreen> {
String url = '';
List data;
@override
void initState() {
super.initState();
this.getJsonData();//call method here
}
Future<http.Request> getJsonData() async {
var response = await http
.get(url, headers: {'auth_key': '7tJBAO3xwIF3UhfIwMYryL2Cqo1dpQ'});//this is my auth_key(token)
print(response.body);
setState(() {
var convertDataToJson = json.decode(response.body);
data = convertDataToJson['results'];
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Courses"),
),
body: new ListView.builder(
itemCount: data == null ? 0 : data.length,
itemBuilder: (BuildContext context, int index) {
return Container(
child: Center(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Card(
child: Container(
child: Text(data[index]['courseName']),
padding: EdgeInsets.all(20.0),
),
)
],
),
),
);
},
),
);
}
}
运行此应用程序后...
Xcode 构建完成。14.3s 安装和启动...正在将文件同步到设备 Samcom Tech 的 iPhone...flutter: {"error":true,"message":"Sorry, Auth key is not defined"} [在此处输入图像描述][1]