如何收听提供商的更改?

时间:2020-09-10 19:34:13

标签: flutter dart

我正在构建一个允许用户将动漫流式传输到手机的应用程序。但是,我很难按照Provider模式来构建应用程序。

这是我希望我的应用执行的操作:

  • 我想从twist.moe网站获取所有动漫,
  • 我想使用提供者收听所有动漫,
  • 我希望能够使用提供程序访问动漫列表。

search.dart文件存在错误,我无法访问标题。

我的代码:

providers / anime.dart

import 'package:anime_go/models/anime_all.dart';
import 'package:anime_go/services/anime_twist.dart';
import 'package:flutter/material.dart';

class AnimeModel extends ChangeNotifier {
  final AnimeTwistApiService api = AnimeTwistApiService();

  final List<List<AnimeAll>> allAnimeList = [];

  void addAnimeList() async {
    final List<AnimeAll> animeList = await api.getAllAnime();

    allAnimeList.add(animeList);
    notifyListeners();
  }
}

models / anime_all.dart

import 'dart:convert';

List<AnimeAll> animeAllFromJson(String str) =>
    List<AnimeAll>.from(json.decode(str).map((x) => AnimeAll.fromJson(x)));

String animeAllToJson(List<AnimeAll> data) =>
    json.encode(List<dynamic>.from(data.map((x) => x.toJson())));

class AnimeAll {
  AnimeAll({
    this.id,
    this.title,
    this.altTitle,
    this.season,
    this.ongoing,
    this.hbId,
    this.createdAt,
    this.updatedAt,
    this.hidden,
    this.malId,
    this.slug,
  });

  int id;
  String title;
  String altTitle;
  int season;
  int ongoing;
  int hbId;
  DateTime createdAt;
  DateTime updatedAt;
  int hidden;
  int malId;
  Slug slug;

  factory AnimeAll.fromJson(Map<String, dynamic> json) => AnimeAll(
        id: json["id"],
        title: json["title"],
        altTitle: json["alt_title"] == null ? null : json["alt_title"],
        season: json["season"],
        ongoing: json["ongoing"],
        hbId: json["hb_id"] == null ? null : json["hb_id"],
        createdAt: DateTime.parse(json["created_at"]),
        updatedAt: DateTime.parse(json["updated_at"]),
        hidden: json["hidden"],
        malId: json["mal_id"] == null ? null : json["mal_id"],
        slug: Slug.fromJson(json["slug"]),
      );

  Map<String, dynamic> toJson() => {
        "id": id,
        "title": title,
        "alt_title": altTitle == null ? null : altTitle,
        "season": season,
        "ongoing": ongoing,
        "hb_id": hbId == null ? null : hbId,
        "created_at": createdAt.toIso8601String(),
        "updated_at": updatedAt.toIso8601String(),
        "hidden": hidden,
        "mal_id": malId == null ? null : malId,
        "slug": slug.toJson(),
      };
}

class Slug {
  Slug({
    this.id,
    this.slug,
    this.animeId,
    this.createdAt,
    this.updatedAt,
  });

  int id;
  String slug;
  int animeId;
  DateTime createdAt;
  DateTime updatedAt;

  factory Slug.fromJson(Map<String, dynamic> json) => Slug(
        id: json["id"],
        slug: json["slug"],
        animeId: json["anime_id"],
        createdAt: DateTime.parse(json["created_at"]),
        updatedAt: DateTime.parse(json["updated_at"]),
      );

  Map<String, dynamic> toJson() => {
        "id": id,
        "slug": slug,
        "anime_id": animeId,
        "created_at": createdAt.toIso8601String(),
        "updated_at": updatedAt.toIso8601String(),
      };
}

services / anime_twist.dart

import 'package:anime_go/models/anime_all.dart';
import 'package:anime_go/.env.dart';
import 'package:http/http.dart' as http;

class AnimeTwistApiService {
  static const baseUrl = 'https://twist.moe/api/anime';

  Future<List<AnimeAll>> getAllAnime() async {
    final http.Response response = await http
        .get(baseUrl, headers: {'x-access-token': EnvironmentVariables.token});

    return animeAllFromJson(response.body);
  }
}

pages / tabs / search.dart

import 'package:anime_go/providers/anime.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class SearchTab extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final animeList = context.watch<AnimeModel>().addAnimeList();
    return Text(animeList.title.toString()); // here's the error, I can't access "title"
  }
}

main.dart

import 'package:anime_go/pages/home.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:anime_go/providers/anime.dart';

void main() {
  runApp(
    EasyLocalization(
        supportedLocales: [Locale('en', 'US')],
        path: 'lib/assets/translations',
        fallbackLocale: Locale('en', 'US'),
        child: AnimeGo()),
  );
}

class AnimeGo extends StatelessWidget {
  final AnimeModel _animeModel = AnimeModel();

  AnimeGo() {
    _animeModel.addAnimeList();
  }

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(
          create: (context) => AnimeModel(),
        )
      ],
      child: MaterialApp(
        localizationsDelegates: context.localizationDelegates,
        supportedLocales: context.supportedLocales,
        locale: context.locale,
        debugShowCheckedModeBanner: false,
        theme: ThemeData(
          primaryColor: Colors.blueGrey[800],
        ),
        title: 'title'.tr(),
        home: HomeScreen(),
      ),
    );
  }
}

1 个答案:

答案 0 :(得分:0)

您将返回List 的AnimeModel对象。

标题属于AnimeAll。您首先需要像这样在返回的对象内部访问它:

AnimeList[index_1][index_2].title