我正在开发一个应用程序,用于使用提供程序模式观看动漫。
但是,我很难使用它。
该错误显示在pages / tabs / search.dart
中我无法访问从提供商获取的“ title”参数。
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(),
),
);
}
}
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);
}
}
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();
}
}
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
],
);
},
);
}
}
答案 0 :(得分:0)
您的allAnimeList
是List<List<AnimeAll>>
。因此,当您在for
循环中对其进行迭代时,现在一个anime
的类型为List<AnimeAll>
。此类型确实没有title
属性。
我不知道你想要。也许您想再次遍历此列表 然后然后使用每个AnimeAll
的title属性?
说实话,这对我来说很令人困惑,您有一个名为... All的列表(对我来说这也表示这也是一个集合类型),看来您自己很困惑,也许是时候考虑更好的数据结构和命名了。