Home > database >  dart does not see the variable declared in the same file
dart does not see the variable declared in the same file

Time:01-04

Dart does not see the variable _movies declared in the same file below is the code of the widget which gives the only error : Undefined name '_movies'. Try correcting the name to one that is defined, or defining the name.dart

import 'package:flutter/material.dart';
import '../../resources/resources.dart';

class Movie {
  final String imageName;
  final String title;
  final String time;
  final String description;

  Movie({
    required this.imageName,
    required this.title,
    required this.time,
    required this.description,
  });
}

class MovieListWidget extends StatefulWidget {
  final _movies = [
    Movie(
        imageName: AppImages.moviePlacholder,
        title: 'TOP GUN',
        time: 'April 7, 2021',
        description:
            'Fhjshfkhfdkhskjhkjsdhfkjsdhfjhsdfkjhsdkjfhskjdhfkjsdhfjsdhfkjhsdfjhsdkjfhskhdfksdhfkjdshfkjhsdkjfhskjdfhskjdhfkjshdfkjhsdkjfhskjdfhksjhfjdhgfkjshgkjdhjghfjksdhkjgfdhgjkhdgkjhdskghsfjghsdfhgairhe')
  ];

  MovieListWidget({super.key});

  @override
  State<MovieListWidget> createState() => _MovieListWidgetState();
}

class _MovieListWidgetState extends State<MovieListWidget> {
  @override
  Widget build(BuildContext context) {
    return ListView.builder(
        itemCount: _movies,
        itemExtent: 163,
        itemBuilder: (BuildContext context, int index) {
          return Padding(
            padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 10),
            child: Stack(
              children: [
                Container(
                  decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: const BorderRadius.all(Radius.circular(10)),
                      border: Border.all(color: Colors.black.withOpacity(0.2)),
                      boxShadow: [
                        BoxShadow(
                            color: Colors.black.withOpacity(0.1),
                            blurRadius: 8,
                            offset: const Offset(2, 2)),
                      ]),
                  clipBehavior: Clip.hardEdge,
                  child: Row(
                    children: [
                      const Image(image: AssetImage(AppImages.moviePlacholder)),
                      Expanded(
                        child: Padding(
                          padding: const EdgeInsets.symmetric(horizontal: 20),
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: const [
                              SizedBox(height: 20),
                              Text(
                                'Top Gun',
                                maxLines: 1,
                                overflow: TextOverflow.ellipsis,
                                style: TextStyle(fontWeight: FontWeight.bold),
                              ),
                              SizedBox(height: 5),
                              Text(
                                'April 7, 2021',
                                maxLines: 1,
                                overflow: TextOverflow.ellipsis,
                                style: TextStyle(color: Colors.grey),
                              ),
                              SizedBox(height: 20),
                              Text(
                                'Fhjshfkhfdkhskjhkjsdhfkjsdhfjhsdfkjhsdkjfhskjdhfkjsdhfjsdhfkjhsdfjhsdkjfhskhdfksdhfkjdshfkjhsdkjfhskjdfhskjdhfkjshdfkjhsdkjfhskjdfhksjhfjdhgfkjshgkjdhjghfjksdhkjgfdhgjkhdgkjhdskghsfjghsdfhgairhe;uhufghrhglkzhglkzhlk/hzionvonrb;jhkjhfjkshdfjhjhsjhdfkjshdfjhdjfhskfskd;ljgkjfslkfhklsdhkjvhkjbgnivbfjdhdujfkxjhfjilzxhfjklxhflkjhznriouvboiurzb;ogjoijzgoizhdfoghzoikjshfkjshdfjshfshdkj',
                                maxLines: 2,
                                overflow: TextOverflow.ellipsis,
                              ),
                            ],
                          ),
                        ),
                      )
                    ],
                  ),
                ),
                Material(
                  color: Colors.transparent,
                  child: InkWell(
                    borderRadius: BorderRadius.circular(10),
                    onTap: () {
                      debugPrint('URURURURU');
                    },
                  ),
                )
              ],
            ),
          );
        });
  }
}

I don’t understand why this is happening, maybe it is due to the new rules of the language or framework

CodePudding user response:

_movies is widget class property, to access it on state class, you need to use widget.variableName

   return ListView.builder(
        itemCount: widget._movies.length,

CodePudding user response:

Access it like this,

widget.movies

Because it is inside MovieListWidget.

If it was inside _MovieListWidgetState, you could have accessed movies normally.

  • Related