类“ List <AnimeAll>”没有实例获取器“ title”

时间:2020-09-11 11:20:31

标签: flutter dart

我正在开发一个应用程序,用于使用提供程序模式观看动漫。

但是,我很难使用它。

该错误显示在pages / tabs / search.dart

我无法访问从提供商获取的“ title”参数。

我收到此错误: Image

代码:

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

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);
  }
}

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 allAnimeList = [];

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

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

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) {
    return Consumer<AnimeModel>(
      builder: (context, animeList, child) {
        print(animeList.allAnimeList.toString());
        return Column(
          children: [
            for (var anime in animeList.allAnimeList) Text(anime.title) // here is the problem - the error
          ],
        );
      },
    );
  }
}

1 个答案:

答案 0 :(得分:0)

您的allAnimeListList<List<AnimeAll>>。因此,当您在for循环中对其进行迭代时,现在一个anime的类型为List<AnimeAll>。此类型确实没有title属性。

我不知道你想要。也许您想再次遍历此列表 然后然后使用每个AnimeAll的title属性?

说实话,这对我来说很令人困惑,您有一个名为... All的列表(对我来说这也表示这也是一个集合类型),看来您自己很困惑,也许是时候考虑更好的数据结构和命名了。