我有用于提取列表的auth_key。
我尝试了很多次,但是auth_key没有进入第二个屏幕,因为我无法从互联网上获取数据 我在Postman中使用JSON请求标头测试了API,并且看起来工作正常。
这是回应... { “错误”:错误, “ message”:“成功”, “ course_list”:[ { “ courseId”:“ 19”, “ courseName”:“ Verbs-01-def-prctice”, “ price”:“ 0”, “ sub_type”:“ 1”, “ sku_id”:“ 15464”, “ courseIcon”:“”, “ type”:“ 0”, “付费”:false }
**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.3秒 安装和启动... 正在将文件同步到设备Samcom Tech的iPhone ... 颤动:{“错误”:true,“消息”:“对不起,未定义Auth键”} [在此处输入图片描述] [1]
答案 0 :(得分:0)
通常,您需要在标题中使用“授权”而不是“ auth_key”。请尝试一下。或者,请尝试以下示例:
Map<String, String> headers = {
HttpHeaders.authorizationHeader: "7tJBAO3xwIF3UhfIwMYryL2Cqo1dpQ",
HttpHeaders.contentTypeHeader: "application/json"
};
var response = await http
.get(url, headers: headers);
答案 1 :(得分:0)
如果您希望对HTTP请求有一个强大的库,称为CHOPPER: https://pub.dev/packages/chopper
您还可以找到有关以下内容的出色教程: https://www.youtube.com/watch?v=zFXK5EsrUF0&t=1553s
这是一个代码示例:
// my_service.dart
import "dart:async";
import 'package:chopper/chopper.dart';
part "my_service.chopper.dart";
@ChopperApi(baseUrl: "/resources")
abstract class MyService extends ChopperService {
static MyService create([ChopperClient client]) => _$MyService(client);
@Get(url: "{id}")
Future<Response> getResource(@Path() String id);
@Get(headers: const {"foo": "bar"})
Future<Response<Map>> getMapResource(@Query() String id);
@Post(url: 'multi')
@multipart
Future<Response> postResources(
@Part('1') Map a,
@Part('2') Map b,
@Part('3') String c,
);
@Post(url: 'file')
@multipart
Future<Response> postFile(
@FileField('file') List<int> bytes,
);
}
答案 2 :(得分:0)
请尝试以下代码:
response = http.get(URL, headers: {
"Accept": "application/json",
"Content-Type": "application/x-www-form-urlencoded"
"auth_key': '7tJBAO3xwIF3UhfIwMYryL2Cqo1dpQ"
}).then((http.Response response) {});