How do i solve this error : "error":"Bad Request" in imgur api flutter
How do i solve this error : "error":"Bad Request" in imgur api flutter
我正在处理一项任务,它是 imgur 的克隆。首先,我想制作一个显示来自 imgur 的图像的仪表板。
我想在我的仪表板中获取图片,但我收到一条错误消息:
flutter: {"data":{"error":"Bad Request","request":"/3/gallery/","method":"GET"},"success":false,"状态":400}
你能检查我的代码并告诉我我哪里做错了吗?
@override
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
var mPageCount = 0; // initial page count will be 0
bool isLoading = false;
int itemType = ImgurImage.TYPE_PROGRESS;
List<ImgurImage> imageList = [];
ScrollController? _controller;
_scrollListener() {
if (_controller!.offset >= _controller!.position.maxScrollExtent &&
!_controller!.position.outOfRange) {
_fetchImages();
}
}
@override
void initState() {
_controller = ScrollController();
_controller!.addListener(_scrollListener);
super.initState();
_fetchImages();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: _loadView(),
);
}
Widget _loadView() {
if (imageList.isEmpty ||
(imageList.length == 1 &&
imageList[0].itemType == ImgurImage.TYPE_PROGRESS)) {
return const Center(child: CircularProgressIndicator());
} else if (imageList.length == 1 &&
imageList[0].itemType == ImgurImage.TYPE_ERROR) {
return Center(
child: RaisedButton(
onPressed: () {
_fetchImages();
},
child: Text('Try Again'),
),
);
} else {
itemType = imageList[imageList.length - 1].itemType;
return Column(
children: <Widget>[
Expanded(
child: GridView.count(
crossAxisCount: 3,
controller: _controller,
children: List.generate(
imageList.length,
(index) {
var image = imageList[index];
if (image.itemType == ImgurImage.TYPE_ITEM) {
return Center(
child: FadeInImage.assetNetwork(
placeholder: 'assets/imgur_placeholder.jpg',
image: image.link));
} else {
return Container();
}
},
),
),
),
_showIndicator(),
],
);
}
}
Widget _showIndicator() {
if (itemType == ImgurImage.TYPE_PROGRESS) {
return Container(
margin: const EdgeInsets.all(20),
child: const Center(child: CircularProgressIndicator()),
);
} else {
return Container();
}
}
void _fetchImages() async {
if (!isLoading) {
mPageCount++;
isLoading = true;
if (imageList.length == 1) imageList.removeLast();
imageList.add(ImgurImage(link: "", itemType: ImgurImage.TYPE_PROGRESS));
setState(() {});
await fetchImages().then((imgurImages) {
imageList.removeLast();
for (var value in imgurImages.images!) {
imageList.add(value);
}
}).catchError((error) {
imageList.removeLast();
if (imageList.isEmpty) {
imageList.add(ImgurImage(link: "", itemType: ImgurImage.TYPE_ERROR));
}
if (mPageCount > 0) {
mPageCount--;
}
}).whenComplete(() {
isLoading = false;
setState(() {});
});
}
}
}
看来你的url不对
这是图库端点 url 规范。
https://api.imgur.com/endpoints/gallery#gallery
我正在处理一项任务,它是 imgur 的克隆。首先,我想制作一个显示来自 imgur 的图像的仪表板。
我想在我的仪表板中获取图片,但我收到一条错误消息:
flutter: {"data":{"error":"Bad Request","request":"/3/gallery/","method":"GET"},"success":false,"状态":400}
你能检查我的代码并告诉我我哪里做错了吗?
@override
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
var mPageCount = 0; // initial page count will be 0
bool isLoading = false;
int itemType = ImgurImage.TYPE_PROGRESS;
List<ImgurImage> imageList = [];
ScrollController? _controller;
_scrollListener() {
if (_controller!.offset >= _controller!.position.maxScrollExtent &&
!_controller!.position.outOfRange) {
_fetchImages();
}
}
@override
void initState() {
_controller = ScrollController();
_controller!.addListener(_scrollListener);
super.initState();
_fetchImages();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: _loadView(),
);
}
Widget _loadView() {
if (imageList.isEmpty ||
(imageList.length == 1 &&
imageList[0].itemType == ImgurImage.TYPE_PROGRESS)) {
return const Center(child: CircularProgressIndicator());
} else if (imageList.length == 1 &&
imageList[0].itemType == ImgurImage.TYPE_ERROR) {
return Center(
child: RaisedButton(
onPressed: () {
_fetchImages();
},
child: Text('Try Again'),
),
);
} else {
itemType = imageList[imageList.length - 1].itemType;
return Column(
children: <Widget>[
Expanded(
child: GridView.count(
crossAxisCount: 3,
controller: _controller,
children: List.generate(
imageList.length,
(index) {
var image = imageList[index];
if (image.itemType == ImgurImage.TYPE_ITEM) {
return Center(
child: FadeInImage.assetNetwork(
placeholder: 'assets/imgur_placeholder.jpg',
image: image.link));
} else {
return Container();
}
},
),
),
),
_showIndicator(),
],
);
}
}
Widget _showIndicator() {
if (itemType == ImgurImage.TYPE_PROGRESS) {
return Container(
margin: const EdgeInsets.all(20),
child: const Center(child: CircularProgressIndicator()),
);
} else {
return Container();
}
}
void _fetchImages() async {
if (!isLoading) {
mPageCount++;
isLoading = true;
if (imageList.length == 1) imageList.removeLast();
imageList.add(ImgurImage(link: "", itemType: ImgurImage.TYPE_PROGRESS));
setState(() {});
await fetchImages().then((imgurImages) {
imageList.removeLast();
for (var value in imgurImages.images!) {
imageList.add(value);
}
}).catchError((error) {
imageList.removeLast();
if (imageList.isEmpty) {
imageList.add(ImgurImage(link: "", itemType: ImgurImage.TYPE_ERROR));
}
if (mPageCount > 0) {
mPageCount--;
}
}).whenComplete(() {
isLoading = false;
setState(() {});
});
}
}
}
看来你的url不对
这是图库端点 url 规范。
https://api.imgur.com/endpoints/gallery#gallery