Class 'List<AnimeAll>' has no instance getter 'title' - flutter

I'm building an app in flutter for watching anime using provider pattern.
However, I have a hard time working with it.
The error shows up in pages/tabs/search.dart
I can't access the "title" parameter fetching from provider.
I got this error:
Code:
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
],
);
},
);
}
}

Your allAnimeList is a List<List<AnimeAll>>. So when you iterate over it in your for loop, one anime is now of type List<AnimeAll>. This type does indeed not have a title property.
I don't know what you wanted. Maybe you want to iterate over this list again and then use the title property of each AnimeAll?
To be honest it's quite confusing to me, you have a list of a list of something called an ...All (that to me would signal that this is a collection type as well), it seems you got confused yourself, maybe it's time to think about a better structure and naming of your data.

Related

Unhandled Exception: PlatformException(null-error, Host platform returned null value for non-null return value., null, null)

hello I hope you are well, I have the following problem, when I run my app in flutter I get the following ERROR: E/flutter ( 6922): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException( null-error, Host platform returned null value for non-null return value., null, null), as I am working with firebase, but I have searched a lot and the truth is that the solutions that come up have not worked for me or I do not know if it is me who I implemented it incorrectly, soon I will show you the code, thank you very much for stopping to read this publication. I also forgot to mention I'm working on visual studio code
MAIN CODE(),
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'src/Base/Views/BaseView.dart';
import 'src/Feactures/Presentation/Shared/StateProviders/LoadingStateProvider.dart';
import 'src/Feactures/Presentation/Shared/StateProviders/UserStateProvider.dart';
import 'src/Colors/colors.dart';
import 'src/Routes/routes.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:firebase_core/firebase_core.dart';
void main() => runApp(AppState());
class AppState extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider(create: (_) => LoadingStateProvider()),
ChangeNotifierProvider(create: (_) => UserStateProvider())
],
child: MyAppUserState(),
);
}
}
class MyAppUserState extends StatelessWidget with BaseView {
#override
Widget build(BuildContext context) {
return FutureBuilder(
future: coordinator.start(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasData) {
return MyApp(initialRoute: snapshot.data);
} else {
return CircularProgressIndicator();
}
});
}
}
class MyApp extends StatelessWidget {
final String _initialRoute;
MyApp({required String initialRoute}) : _initialRoute = initialRoute;
#override
Widget build(BuildContext context) {
Firebase.initializeApp();
return MaterialApp(
debugShowCheckedModeBanner: false,
routes: routes,
initialRoute: _initialRoute,
theme: ThemeData(
scaffoldBackgroundColor: Colors.white,
primaryColor: orange,
appBarTheme:
const AppBarTheme(iconTheme: IconThemeData(color: Colors.black))),
localizationsDelegates: const [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: const [
Locale('en', ''), // English, no country code
Locale('es', ''), // Spanish, no country code
],
);
}
}
I really expected the app to run fine but it sends that error
You probably don't want to initialize Firebase in the build method. Try doing this instead (which is in the FlutterFire documentation):
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp();
}
Add the following plugin of the recent/latest version in the pubspec.yaml:
firebase_core :recent version
Please do the following changes in main.dart file.
Hope it works!!
import 'package:firebase_core/firebase_core.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(AppState());
}

(watch)The expression doesn't evaluate to a function, so it can't be invoked

I'm trying to set up a theme for my flutter project, ill greatly appreciate if you kindly show me another way to do that.
import 'package:firebase_analytics/firebase_analytics.dart';
import 'package:firebase_analytics/observer.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:picpuzzle/routes/route_name.dart';
import 'package:picpuzzle/widgets/login/login_screen.dart';
import 'package:picpuzzle/widgets/main/main_page.dart';
import 'package:picpuzzle/widgets/puzzle/puzzle_list_page.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'common/constants.dart';
/// use for DarkMode,not sync with system
final DarkModeProvider = StateProvider((ref) => false);
class MyApp extends StatelessWidget {
// This widget is the root of your application.
static FirebaseAnalytics analytics = FirebaseAnalytics.instance;
static FirebaseAnalyticsObserver observer =
FirebaseAnalyticsObserver(analytics: analytics);
void initApp(BuildContext context) {}
#override
Widget build(BuildContext context) {
/// init
initApp(context);
return Consumer(
builder: (context, watch, _) {
return MaterialApp(
debugShowCheckedModeBanner: false,
localizationsDelegates: [
AppLocalizations.delegate, // Add this line
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: [
const Locale('en', ''), // English, no country code
const Locale('ja', ''), // Japanese, no country code
],
title: 'Pic Puzzle',
initialRoute: RouteName.Login,
routes: {
RouteName.Login: (context) => LoginScreen(),
RouteName.Main: (context) => MainPage(),
RouteName.Home: (context) => PuzzleListPage(),
},
theme: watch(DarkModeProvider).state ? darkTheme : lightTheme,
// darkTheme: darkTheme,
);
},
);
}
}
but it won't be able to make it ,becouse of this
lib/my_app.dart:49:23: Error: The method 'call' isn't defined for the
class 'WidgetRef'.
- 'WidgetRef' is from 'package:flutter_riverpod/src/consumer.dart'
('../Documents/flutter_windows_2.8.1-stable/flutter/.pub-cache/hosted/pub.dartlang.org/flutter_riverpod-1.0.3/lib/src/consumer.dart').
Try correcting the name to the name of an existing method, or defining a method named 'call'.
theme: watch(DarkModeProvider).state ? darkTheme : lightTheme,
This part
theme: watch(DarkModeProvider).state ? darkTheme : lightTheme,
should be changed to:
theme: watch.watch(DarkModeProvider.state).state ? darkTheme : lightTheme,

Flutter MultiProvider Not found

Error: Could not find the correct Provider above this AuthenticationWrapper Widget
This happens because you used a BuildContext that does not include the provider
of your choice. There are a few common scenarios:
You added a new provider in your main.dart and performed a hot-reload.
To fix, perform a hot-restart.
The provider you are trying to read is in a different route.
Providers are "scoped". So if you insert of provider inside a route, then
other routes will not be able to access that provider.
You used a BuildContext that is an ancestor of the provider you are trying to read.
Make sure that AuthenticationWrapper is under your MultiProvider/Provider.
This usually happens when you are creating a provider and trying to read it immediately
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:park_app/app_styles.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import './views/pages.dart';
import 'views/authentication/authentication_service.dart';
import 'Home_Page.dart';
bool? seenOnboard;
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
// to show status bar
SystemChrome.setEnabledSystemUIOverlays(
[SystemUiOverlay.bottom, SystemUiOverlay.top]);
// to load onboard for the first time only
SharedPreferences pref = await SharedPreferences.getInstance();
seenOnboard = pref.getBool('seenOnboard') ?? false; //if null set to false
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
Provider<AuthenticationService>(
create: (_) => AuthenticationService(FirebaseAuth.instance),
),
StreamProvider(
initialData: null,
create: (context) =>
context.read<AuthenticationService>().authStateChanges),
],
child: MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Park App',
theme: ThemeData(
textTheme: GoogleFonts.manropeTextTheme(
Theme.of(context).textTheme,
),
primarySwatch: Colors.blue,
scaffoldBackgroundColor: kScaffoldBackground,
),
home: seenOnboard == true ? AuthenticationWrapper() : OnBoardingPage(),
),
);
}
}
class AuthenticationWrapper extends StatelessWidget {
#override
Widget build(BuildContext context) {
final firebaseUser = context.watch<User>();
if (firebaseUser != null) {
return HomePage();
}
return LoginPage();
}
}
install provider by running the command below:
flutter pub add provider and then in your main.dart file, import it import 'package:provider/provider.dart';
Make sure to specify the generic type on StreamProvider:
StreamProvider<User?>(
...
)
Note that you have set null as initialData, so your widget likely needs to handle null users. Meaning you need to do:
final user = context.watch<User?>()

Could not find the correct Provider<User> above this AuthWrapper Widget

I am trying to fix this error
Error: Could not find the correct Provider above this AuthWrapper Widget.
Following this tutorial but it doesnt seem to work for me.https://www.youtube.com/watch?v=yyD_VqSrKd8
here is the code:
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:untitled2/firbaseauth.dart';
import 'package:untitled2/homescreen.dart';
import 'package:untitled2/startpage.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
Provider<Authservis>(
create: (_) => Authservis(FirebaseAuth.instance),
),
StreamProvider(create: (context) => context.read<Authservis>().authStateChanges,
initialData: null,),
],
child: MaterialApp(
title: "APP",
home: AuthWrapper(),
),
);
}
}
class AuthWrapper extends StatelessWidget{
#override
Widget build(BuildContext context) {
final user = context.watch<User>();
if(user != null){
return Homescreen();
}
return Startpage();
}
}
The error exactly tells you that there is no Provider named User above the widget that calls such provider.
Your MultiProvider at the top of your tree includes just a Auhtservis provider, but in order to use your User provider, the list should include the User provider, too.
Also, youtube tutorials aren't perfect, if you lecturer uses this precise code you might want to notify them.
I can't see any Provider with User type above your AuthWrapper. I think the correct code should be this:
final authService = context.watch<AuthService>();
// check if user is logged in
try giving your StreamProvider a type of User
StreamProvider<User>(create: (context) => context.read<Authservis>().authStateChanges,
initialData: null,),

Flutter/Dart variables lost / keep getting reinitialized when trying to get from different class

The variables keep getting reinitialized when accessed from another class.
I have a class, in its separate dart source file, declared as such:
helper_provider.dart
import 'package:flutter/material.dart';
class BuilderHelperProvider extends ChangeNotifier {
var _builderDetails;
void setBuilderDetails(inputDetails) {
this._builderDetails = inputDetails;
}
get builderDetails {
return _builderDetails;
}
}
and I initialize the variable in main.dart
main.dart
import 'package:builder_project/helpers/helper_provider.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider(
create: (context) => BuilderHelperProvider(),
),
],
child: MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: StreamBuilder(
stream: FirebaseFirestore.instance
.collection('builders')
.doc('builder id')
.snapshots(),
builder: (context, builderSnap) {
if (builderSnap.connectionState == ConnectionState.waiting) {
return Center(child: CircularProgressIndicator());
}
final _builderDetails = builderSnap.data;
BuilderHelperProvider().setBuilderDetails(_builderDetails);
print('builder Details: ${BuilderHelperProvider().builderDetails}');
# *This keeps returning null*
return BuilderHomepage();
})
// home: BuilderHomepage(),
),
);
}
}
The output of the above code comes as:
I/flutter (31260): builder Details: null
My application did not quit or stop between the two operations. Under what circumstances would get reinitalised?
just make them static and you will be fine
static var _builderDetails;