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

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

anilist

Unofficial AniList GraphQL API。通过级联操作实现快速且简单的请求和字段选择。


安装

pubspec.yaml 文件中添加 anilist 作为依赖(如何安装?)。

dependencies:
  anilist: ^x.x.x

运行以下命令以安装依赖:

flutter pub get

导入

导入 anilist 包:

import 'package:anilist/anilist.dart';

使用

简单易用!

你可以通过 ID 获取媒体信息,或者搜索媒体、角色、工作人员等。还可以在媒体中嵌套查询角色信息…

目前并非所有功能都已实现。如果你需要某些功能,可以打开一个 Issue,我会尽快添加。


请求媒体信息

以下是通过 ID 请求媒体信息的示例代码:

void main() async {
  // 定义角色和工作人员的选择器
  final charSelect = AnilistCharacterSelect();
  charSelect..withNameFull(); // 选择角色全名
  final staffSelect = AnilistStaffSelect();
  staffSelect..withNameFull(); // 选择工作人员全名

  // 定义媒体请求
  final request = AnilistMediaRequest();
  request
    ..withIdMal() // 添加 MAL ID 字段
    ..withTitle() // 添加标题字段
    ..withType() // 添加类型字段
    ..withFormat() // 添加格式字段
    ..withStatus() // 添加状态字段
    ..withDescription() // 添加描述字段
    ..withStartDate() // 添加开始日期字段
    ..withEndDate() // 添加结束日期字段
    ..withSeason() // 添加季度字段
    ..withCountryOfOrigin() // 添加原产国字段
    ..withIsLicensed() // 添加是否授权字段
    ..withSource() // 添加来源字段
    ..withHashtag() // 添加标签字段
    ..withTrailer() // 添加预告片字段
    ..withUpdatedAt() // 添加更新时间字段
    ..withCoverImage() // 添加封面图字段
    ..withBannerImage() // 添加横幅图字段
    ..withGenres() // 添加分类字段
    ..withSynonyms() // 添加别名字段
    ..withMeanScore() // 添加平均评分字段
    ..withAverageScore() // 添加平均分字段
    ..withPopularity() // 添加受欢迎程度字段
    ..withIsLocked() // 添加锁定字段
    ..withFavourites() // 添加收藏数字段
    ..withTrending() // 添加趋势字段
    ..withTagsId() // 添加标签 ID 字段
    ..withTagsName() // 添加标签名称字段
    ..withCharcters(AnilistSubquery(charSelect, perPage: 5)) // 嵌套角色查询
    ..withStaff(AnilistSubquery(staffSelect, perPage: 5)); // 嵌套工作人员查询

  // 通过 ID 获取媒体信息
  var media = await request.byId(53390); // 替换为实际的 ID
  print(request.query); // 打印生成的 GraphQL 查询
  print(media); // 打印返回的媒体对象

  // 验证返回数据
  expect(media.id, equals(53390));
  expect(media.idMal, equals(23390));
  expect(media.title.english, equals('Attack on Titan'));
  expect(media.title.romaji, equals('Shingeki no Kyojin'));
  expect(media.type, equals(AnilistMediaType.MANGA));
  expect(media.format, equals(AnilistMediaFormat.MANGA));
  expect(media.status, equals(AnilistMediaStatus.RELEASING));
}

搜索媒体

以下是通过关键词搜索媒体的示例代码:

void main() async {
  final request = AnilistMediaRequest();
  request..withGenres(); // 添加分类字段
  request
    ..querySearch('attack') // 设置搜索关键词
    ..queryGenres(['comedy', 'action']); // 设置分类过滤条件

  // 获取搜索结果
  var result = await request.list(1, 1); // 第一个参数为页码,第二个参数为每页数量
  print(result.results);

  // 验证返回数据
  expect(result.results, hasLength(1));
  expect(result.results.first.genres, contains('Comedy'));
  expect(result.results.first.genres, contains('Action'));
}

请求工作人员信息

以下是通过 ID 请求工作人员信息的示例代码:

void main() async {
  final request = AnilistStaffRequest();
  request..withName(); // 添加名字字段

  // 获取工作人员信息
  var staff = await request.byId(106705); // 替换为实际的工作人员 ID
  print(staff);

  // 验证返回数据
  expect(staff.name.first, equals('Hajime'));
}

示例代码

以下是完整的示例代码,可以在 GitHub 上找到:

import 'package:anilist/anilist.dart';

void main() async {
  final charSelect = AnilistCharacterSelect();
  charSelect..withNameFull(); // 选择角色全名
  final staffSelect = AnilistStaffSelect();
  staffSelect..withNameFull(); // 选择工作人员全名

  // 定义媒体请求
  final request = AnilistMediaRequest();
  request
    ..withIdMal()
    ..withTitle()
    ..withType()
    ..withFormat()
    ..withStatus()
    ..withDescription()
    ..withStartDate()
    ..withEndDate()
    ..withSeason()
    ..withCountryOfOrigin()
    ..withIsLicensed()
    ..withSource()
    ..withHashtag()
    ..withTrailer()
    ..withUpdatedAt()
    ..withCoverImage()
    ..withBannerImage()
    ..withGenres()
    ..withSynonyms()
    ..withMeanScore()
    ..withAverageScore()
    ..withPopularity()
    ..withIsLocked()
    ..withFavourites()
    ..withTrending()
    ..withTagsId()
    ..withTagsName()
    ..withCharcters(AnilistSubquery(charSelect, perPage: 5))
    ..withStaff(AnilistSubquery(staffSelect, perPage: 5));

  // 通过 ID 获取媒体信息
  var media = await request.byId(53390); // 替换为实际的 ID
  print(request.query); // 打印生成的 GraphQL 查询
  print(media); // 打印返回的媒体对象
}

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

1 回复

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


在Flutter中,你可以使用 anilist 插件来获取动漫信息。anilist 是一个非官方的 AniList API 客户端,它允许你通过 AniList 的 GraphQL API 查询动漫、漫画、角色等信息。

1. 添加依赖

首先,你需要在 pubspec.yaml 文件中添加 anilist 插件的依赖:

dependencies:
  flutter:
    sdk: flutter
  anilist: ^0.1.0  # 请检查最新版本

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

2. 使用 anilist 插件

接下来,你可以在你的 Flutter 应用中使用 anilist 插件来获取动漫信息。

示例:获取动漫信息

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

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'AniList Example',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: AnimeInfoScreen(),
    );
  }
}

class AnimeInfoScreen extends StatefulWidget {
  [@override](/user/override)
  _AnimeInfoScreenState createState() => _AnimeInfoScreenState();
}

class _AnimeInfoScreenState extends State<AnimeInfoScreen> {
  Anime? anime;

  [@override](/user/override)
  void initState() {
    super.initState();
    fetchAnimeInfo();
  }

  Future<void> fetchAnimeInfo() async {
    final anilist = Anilist();
    final response = await anilist.query('''
      query {
        Media(id: 1, type: ANIME) {
          id
          title {
            romaji
            english
            native
          }
          description
          episodes
          status
          coverImage {
            large
          }
        }
      }
    ''');

    if (response != null && response['Media'] != null) {
      setState(() {
        anime = Anime.fromJson(response['Media']);
      });
    }
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Anime Info'),
      ),
      body: Center(
        child: anime == null
            ? CircularProgressIndicator()
            : Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  if (anime!.coverImage != null)
                    Image.network(anime!.coverImage!),
                  Text('Title: ${anime!.title.romaji}'),
                  Text('Episodes: ${anime!.episodes}'),
                  Text('Status: ${anime!.status}'),
                  Text('Description: ${anime!.description}'),
                ],
              ),
      ),
    );
  }
}

class Anime {
  final int id;
  final Title title;
  final String description;
  final int? episodes;
  final String status;
  final String? coverImage;

  Anime({
    required this.id,
    required this.title,
    required this.description,
    this.episodes,
    required this.status,
    this.coverImage,
  });

  factory Anime.fromJson(Map<String, dynamic> json) {
    return Anime(
      id: json['id'],
      title: Title.fromJson(json['title']),
      description: json['description'],
      episodes: json['episodes'],
      status: json['status'],
      coverImage: json['coverImage']['large'],
    );
  }
}

class Title {
  final String romaji;
  final String english;
  final String native;

  Title({
    required this.romaji,
    required this.english,
    required this.native,
  });

  factory Title.fromJson(Map<String, dynamic> json) {
    return Title(
      romaji: json['romaji'],
      english: json['english'],
      native: json['native'],
    );
  }
}
回到顶部