Flutter动漫信息获取插件jikan_api的使用

发布于 1周前 作者 itying888 来自 Flutter

Flutter动漫信息获取插件jikan_api的使用

简介

Jikan 是一个 非官方 的 MyAnimeList API。它通过抓取网站数据来满足对完整API的需求,而这些是MyAnimeList所缺乏的。jikan_api 是基于Jikan的一个Dart包,适用于Flutter项目,能够帮助开发者轻松地获取和操作动漫、漫画以及相关人物的信息。

功能概述

Anime(动漫)

Future<Anime> getAnime(int id);
Future<BuiltList<CharacterMeta>> getAnimeCharacters(int id);
Future<BuiltList<PersonMeta>> getAnimeStaff(int id);
Future<BuiltList<Episode>> getAnimeEpisodes(int id, {int page = 1});
Future<BuiltList<Article>> getAnimeNews(int id, {int page = 1});
Future<BuiltList<Forum>> getAnimeForum(int id, {ForumType? type});
Future<BuiltList<Promo>> getAnimeVideos(int id);
Future<BuiltList<Picture>> getAnimePictures(int id);
Future<Stats> getAnimeStatistics(int id);
Future<String> getAnimeMoreInfo(int id);
Future<BuiltList<Recommendation>> getAnimeRecommendations(int id);
Future<BuiltList<UserUpdate>> getAnimeUserUpdates(int id, {int page = 1});
Future<BuiltList<Review>> getAnimeReviews(int id, {int page = 1});

Manga(漫画)

Future<Manga> getManga(int id);
Future<BuiltList<CharacterMeta>> getMangaCharacters(int id);
Future<BuiltList<Article>> getMangaNews(int id, {int page = 1});
Future<BuiltList<Forum>> getMangaForum(int id, {ForumType? type});
Future<BuiltList<Picture>> getMangaPictures(int id);
Future<Stats> getMangaStatistics(int id);
Future<String> getMangaMoreInfo(int id);
Future<BuiltList<Recommendation>> getMangaRecommendations(int id);
Future<BuiltList<UserUpdate>> getMangaUserUpdates(int id, {int page = 1});
Future<BuiltList<Review>> getMangaReviews(int id, {int page = 1});

People(人物)

Future<Person> getPerson(int id);
Future<BuiltList<Picture>> getPersonPictures(int id);

Characters(角色)

Future<Character> getCharacter(int id);
Future<BuiltList<Picture>> getCharacterPictures(int id);

Search(搜索)

Future<BuiltList<Anime>> searchAnime(
    {String? query, AnimeType? type, List<int>? genres, List<int>? producers, String? orderBy, String? sort, int page = 1});
Future<BuiltList<Manga>> searchManga(
    {String? query, MangaType? type, List<int>? genres, List<int>? magazines, String? orderBy, String? sort, int page = 1});
Future<BuiltList<Person>> searchPeople({String? query, String? orderBy, String? sort, int page = 1});
Future<BuiltList<Character>> searchCharacters({String? query, String? orderBy, String? sort, int page = 1});

Seasons(季度)

Future<BuiltList<Anime>> getSeason({int? year, SeasonType? season, int page = 1});
Future<BuiltList<Anime>> getSeasonUpcoming({int page = 1});
Future<BuiltList<Archive>> getSeasonsList();

Schedules(排期)

Future<BuiltList<Anime>> getSchedules({WeekDay? weekday, int page = 1});

Top(排行榜)

Future<BuiltList<Anime>> getTopAnime({AnimeType? type, TopFilter? filter, int page = 1});
Future<BuiltList<Manga>> getTopManga({MangaType? type, TopFilter? filter, int page = 1});
Future<BuiltList<Person>> getTopPeople({int page = 1});
Future<BuiltList<Character>> getTopCharacters({int page = 1});
Future<BuiltList<UserReview>> getTopReviews({int page = 1});

Genres(类型)

Future<BuiltList<Genre>> getAnimeGenres({GenreType? type});
Future<BuiltList<Genre>> getMangaGenres({GenreType? type});

Producers(制作公司)

Future<BuiltList<Producer>> getProducers({String? query, String? orderBy, String? sort, int page = 1});

Magazines(杂志)

Future<BuiltList<Magazine>> getMagazines({String? query, String? orderBy, String? sort, int page = 1});

Users(用户)

Future<UserProfile> getUserProfile(String username);
Future<BuiltList<History>> getUserHistory(String username, {HistoryType? type});
Future<BuiltList<Friend>> getUserFriends(String username, {int page = 1});
Future<BuiltList<UserReview>> getUserReviews(String username, {int page = 1});
Future<BuiltList<UserRecommendation>> getUserRecommendations(String username, {int page = 1});

Reviews(评论)

Future<BuiltList<UserReview>> getRecentAnimeReviews({int page = 1});
Future<BuiltList<UserReview>> getRecentMangaReviews({int page = 1});

Recommendations(推荐)

Future<BuiltList<UserRecommendation>> getRecentAnimeRecommendations({int page = 1});
Future<BuiltList<UserRecommendation>> getRecentMangaRecommendations({int page = 1});

Watch(观看)

Future<BuiltList<WatchEpisode>> getWatchEpisodes({bool popular = false});
Future<BuiltList<WatchPromo>> getWatchPromos({bool popular = false});

示例代码

以下是一个简单的例子,展示了如何使用 jikan_api 包来获取排名前20的TV动漫列表:

import 'package:jikan_api/jikan_api.dart';

void main() async {
  // 创建 Jikan 实例
  final jikan = Jikan();
  
  try {
    // 获取排名前20的TV动漫
    final topAnime = await jikan.getTopAnime(type: AnimeType.tv);
    
    // 打印结果
    for (var anime in topAnime) {
      print('Title: ${anime.title}, Episodes: ${anime.episodes}');
    }
  } catch (e) {
    print('Error fetching data: $e');
  }
}

此代码段首先导入了 jikan_api 包,然后创建了一个 Jikan 类的实例。接着调用了 getTopAnime 方法来获取排名前20的TV动漫,并将结果打印出来。如果请求过程中出现任何错误,则会捕获并打印出来。

希望这能帮助你更好地理解和使用 jikan_api 插件!如果有更多问题或需要进一步的帮助,请随时提问。


更多关于Flutter动漫信息获取插件jikan_api的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter动漫信息获取插件jikan_api的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,下面是一个关于如何使用Flutter中的jikan_api插件来获取动漫信息的示例代码。这个插件是一个用于访问Jikan API的Flutter客户端,Jikan API是一个非官方的MyAnimeList API。

首先,你需要在你的Flutter项目中添加jikan_api依赖。打开你的pubspec.yaml文件,并添加以下依赖:

dependencies:
  flutter:
    sdk: flutter
  jikan_api: ^x.y.z  # 请替换为最新版本号

然后运行flutter pub get来安装依赖。

接下来是一个完整的示例代码,用于获取特定动漫的详细信息:

import 'package:flutter/material.dart';
import 'package:jikan_api/jikan_api.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Jikan API Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: AnimeDetailsScreen(),
    );
  }
}

class AnimeDetailsScreen extends StatefulWidget {
  @override
  _AnimeDetailsScreenState createState() => _AnimeDetailsScreenState();
}

class _AnimeDetailsScreenState extends State<AnimeDetailsScreen> {
  String animeTitle = '';
  String animeSynopsis = '';
  String animeImage = '';

  @override
  void initState() {
    super.initState();
    _fetchAnimeDetails('One Piece');  // 你可以将'One Piece'替换为任何你想查询的动漫名称
  }

  Future<void> _fetchAnimeDetails(String animeName) async {
    try {
      final jikanClient = JikanClient();
      final animeDetails = await jikanClient.getAnimeDetails(animeName);

      setState(() {
        animeTitle = animeDetails.title;
        animeSynopsis = animeDetails.synopsis;
        animeImage = animeDetails.images?.jpg?.imageUrl ?? '';
      });
    } catch (e) {
      print('Error fetching anime details: $e');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Anime Details'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Text(
              'Title: $animeTitle',
              style: TextStyle(fontSize: 24),
            ),
            SizedBox(height: 16),
            Image.network(
              animeImage,
              width: double.infinity,
              height: 200,
              fit: BoxFit.cover,
              loadingBuilder: (context, child, loadingProgress) {
                if (loadingProgress == null) return child;
                return Center(child: CircularProgressIndicator(value: loadingProgress.expectedTotalBytes != null
                    ? loadingProgress.cumulativeBytesLoaded / loadingProgress.expectedTotalBytes!
                    : null,));
              },
              errorWidget: (context, url, error) => Icon(Icons.error),
            ),
            SizedBox(height: 16),
            Text(
              'Synopsis: $animeSynopsis',
              style: TextStyle(fontSize: 16),
            ),
          ],
        ),
      ),
    );
  }
}

在这个示例中,我们做了以下几件事:

  1. 依赖添加:在pubspec.yaml文件中添加了jikan_api依赖。
  2. UI构建:使用MaterialApp构建了一个简单的Flutter应用,并定义了一个AnimeDetailsScreen小部件。
  3. 数据获取:在_fetchAnimeDetails函数中,使用JikanClient获取指定动漫的详细信息。
  4. 状态管理:使用setState函数更新UI状态,以显示获取到的动漫信息。

确保你的网络连接正常,因为Jikan API需要从网络获取数据。另外,根据Jikan API的更新,你可能需要调整代码以匹配最新的API响应结构。

回到顶部