我试图与德国国际交易日志一道消费APIC端点,但没有显示任何数据。 当我与邮政员一起尝试时,终点站的工作是正确的,但我不敢肯定什么错误。 我的守则如下:
import package:dio/dio.dart ;
import package:flutter/material.dart ;
import package:tes2/article.dart ;
class ArticleProvider extends ChangeNotifier {
List<Article> articles = [];
final Dio dio = Dio();
Future<List<Article>> getAllArticles() async {
try {
const String url = https://13.210.163.192:8080/users/public/articles ;
Response response = await dio.get(
url,
);
articles = (response.data[ data ][ article ] as List)
.map((e) => Article.fromJson(e))
.toList();
return articles;
} catch (e) {
rethrow;
}
}
}
class Article {
String? id;
String? image;
String? title;
String? author;
String? topic;
int? viewCount;
int? commentCount;
String? date;
bool? saved;
Article({
this.id,
this.image,
this.title,
this.author,
this.topic,
this.viewCount,
this.commentCount,
this.date,
this.saved,
});
Article.fromJson(Map<String, dynamic> json) {
id = json[ id ];
image = json[ image ];
title = json[ title ];
author = json[ author ];
topic = json[ topic ];
viewCount = json[ view_count ];
commentCount = json[ comment_count ];
date = json[ date ];
saved = json[ saved ];
}
}
class ArticleListScreen extends StatelessWidget {
const ArticleListScreen({Key? key});
@override
Widget build(BuildContext context) {
// Register ArticleProvider as a provider above the widget tree
final articleProvider = Provider.of<ArticleProvider>(context);
// Execute fetchArticles when the widget is built
articleProvider.getAllArticles();
return Scaffold(
appBar: AppBar(
title: const Text( Daftar Artikel ),
),
body: Consumer<ArticleProvider>(
builder: (context, articleProvider, _) {
// Check if the article data has been retrieved
if (articleProvider.articles.isEmpty) {
return const Center(child: CircularProgressIndicator());
}
// Display the article data
return ListView.builder(
itemCount: articleProvider.articles.length,
itemBuilder: (context, index) {
return ListTile(
leading:
Image.network(articleProvider.articles[index].image ?? ),
title: Text(articleProvider.articles[index].title ?? ),
subtitle: Text(articleProvider.articles[index].author ?? ),
);
},
);
},
),
);
}
}
谁能帮助我? 我 st着这一错误,我已经尝试过许多辅导员,但似乎没有他们工作。 http://13.210.163.192:80/users/public/articles。 谢谢。