Home > Mobile >  The getter 'duration' isn't defined for the class '_MealItemState'
The getter 'duration' isn't defined for the class '_MealItemState'

Time:05-29

I'm a beginner in flutter I have made some changes by adding createState() and admobe to the original file but I have been stuck in this error:

lib/widgets/meal_item.dart:179:33: Error: The getter 'duration' isn't defined for the class '_MealItemState'.
 - '_MealItemState' is from 'package:oumwalidmaindishes/widgets/meal_item.dart' ('lib/widgets/meal_item.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'duration'.
                      duration: duration,
                                ^^^^^^^^
lib/widgets/meal_item.dart:180:36: Error: The getter 'preparation' isn't defined for the class '_MealItemState'.
 - '_MealItemState' is from 'package:oumwalidmaindishes/widgets/meal_item.dart' ('lib/widgets/meal_item.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'preparation'.
                      preparation: preparation,
                                   ^^^^^^^^^^^
lib/widgets/meal_item.dart:181:31: Error: The getter 'people' isn't defined for the class '_MealItemState'.
 - '_MealItemState' is from 'package:oumwalidmaindishes/widgets/meal_item.dart' ('lib/widgets/meal_item.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'people'.
                      people: people,
                              ^^^^^^
lib/widgets/meal_item.dart:182:35: Error: The getter 'complexity' isn't defined for the class '_MealItemState'.
 - '_MealItemState' is from 'package:oumwalidmaindishes/widgets/meal_item.dart' ('lib/widgets/meal_item.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'complexity'.
                      complexity: complexity))
                                  ^^^^^^^^^^


FAILURE: Build failed with an exception.

* Where:
Script 'C:\src\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 1070

* What went wrong:
Execution failed for task ':app:compileFlutterBuildDebug'.
> Process 'command 'C:\src\flutter\bin\flutter.bat'' finished with non-zero exit value 1

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 17s
Exception: Gradle task assembleDebug failed with exit code 1

When I try to remove this line the error is solved but others appears:

  @override
  State<MealItem> createState() => _MealItemState();

Here is my full code:

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';
import 'package:oumwalidmaindishes/models/ad_helper.dart';
import 'package:provider/provider.dart';

import '../providers/favorite.dart';
import './meal_info.dart';
import '../screens/meal_detail_screen.dart';
import '../models/meal.dart';

const int maxFailedLoadAttempts = 3;

class MealItem extends StatefulWidget {
  final String id;
  final String imageUrl;
  final String title;
  final int preparation;
  final int duration;
  final Complexity complexity;
  final int people;

  const MealItem({
    Key? key,
    required this.id,
    required this.imageUrl,
    required this.people,
    required this.title,
    required this.complexity,
    required this.duration,
    required this.preparation,
  }) : super(key: key);

  @override
  State<MealItem> createState() => _MealItemState();
}

class _MealItemState extends State<MealItem> {
  int _interstitialLoadAttempts = 0;
  InterstitialAd? _interstitialAd;

  void _createInterstitialAd() {
    InterstitialAd.load(
      adUnitId: AdHelper.interstitialAdUnitId,
      request: AdRequest(),
      adLoadCallback: InterstitialAdLoadCallback(
        onAdLoaded: (InterstitialAd ad) {
          _interstitialAd = ad;
          _interstitialLoadAttempts = 0;
        },
        onAdFailedToLoad: (LoadAdError error) {
          _interstitialLoadAttempts  = 1;
          _interstitialAd = null;
          if (_interstitialLoadAttempts <= maxFailedLoadAttempts) {
            _createInterstitialAd();
          }
        },
      ),
    );
  }

  void _showInterstitialAd() {
    if (_interstitialAd != null) {
      _interstitialAd!.fullScreenContentCallback = FullScreenContentCallback(
        onAdDismissedFullScreenContent: (InterstitialAd ad) {
          ad.dispose();
          _createInterstitialAd();
        },
        onAdFailedToShowFullScreenContent: (InterstitialAd ad, AdError error) {
          ad.dispose();
          _createInterstitialAd();
        },
      );
      _interstitialAd!.show();
    }
  }

  @override
  void initState() {
    super.initState();
    _createInterstitialAd();
  }

  @override
  void dispose() {
    super.dispose();
    _interstitialAd?.dispose();
  }

  void selectMeal(BuildContext context) {
    _showInterstitialAd();
    Navigator.of(context)
        .pushNamed(
      MealDetailScreen.routeName,
      arguments: widget.id,
    )
        .then((result) {
      if (result != null) {
        // removeItem(result);
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    final favMeals = Provider.of<Favorite>(context);
    return InkWell(
        onTap: () => selectMeal(context),
        child: Card(
          shape:
              RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)),
          elevation: 10,
          margin: const EdgeInsets.all(15),
          child: Column(
            children: [
              Stack(
                children: [
                  ClipRRect(
                    borderRadius: BorderRadius.circular(20),
                    child: Image.asset(
                      widget.imageUrl,
                      height: 250,
                      width: double.infinity,
                      fit: BoxFit.cover,
                    ),
                  ),
                  Positioned(
                    bottom: 0,
                    left: 0,
                    child: Container(
                      decoration: const BoxDecoration(
                          borderRadius:
                              BorderRadius.only(topRight: Radius.circular(20), bottomLeft: Radius.circular(20)),
                        color: Color(0xfff2f2f2),),
                      width: 300,
                      // color: Colors.black45,
                      padding: const EdgeInsets.symmetric(
                        horizontal: 30,
                        vertical: 5,
                      ),
                      child: Text(
                        widget.title,
                        style: const TextStyle(
                          color: Colors.black87,
                          fontSize: 22,
                        ),
                        softWrap: true,
                        overflow: TextOverflow.fade,
                      ),
                    ),
                  ),
                  Positioned(
                    bottom: 10,
                    right: 5,
                    child: SizedBox(
                      width: 50,
                      height: 50,
                      child: FittedBox(
                        child: FloatingActionButton(
                            heroTag: null,
                            backgroundColor: Colors.white,
                            onPressed: () {
                              favMeals.toggleFavorite(widget.id);
                            },
                            child: favMeals.isMealFavorite(widget.id)
                                ? const Icon(
                                    Icons.favorite_rounded,
                                    color: Colors.pink,
                                  )
                                : const Icon(Icons.favorite_border_rounded)),
                      ),
                    ),
                  )
                ],
              ),
              Padding(
                  padding: const EdgeInsets.all(20.0),
                  child: MealInfo(
                      duration: duration,
                      preparation: preparation,
                      people: people,
                      complexity: complexity))
            ],
          ),
        ));
  }
}

CodePudding user response:

To access the duration inside the _MealItemState, you need to use the widget property so:

this.widget.duration
  • Related