Home > Mobile >  Error: type 'String' is not a subtype of type 'int' of 'index'
Error: type 'String' is not a subtype of type 'int' of 'index'

Time:09-27

import 'package:film_library/utils/text.dart';
import 'package:flutter/material.dart';

class TrendingMovies extends StatelessWidget {
  const TrendingMovies({Key? key, required this.trending}) : super(key: key);
  final List trending;
  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.all(10),
      child: Column(

        crossAxisAlignment: CrossAxisAlignment.start,


        children: [
          const modified_text (text:'Trending Movies', size: 30, color: Colors.white,),
          SizedBox(height: 300,
            child: ListView.builder(itemCount: trending.length,
                scrollDirection: Axis.horizontal,
                itemBuilder: (context,index){
              return InkWell(
                onTap: (){

                },
                child: SizedBox(
                  width: 140,
                  child: Column(
                    children:  [
                      Container(
                        height: 200,
                        decoration:  BoxDecoration(
                         image: DecorationImage(image: NetworkImage(
                             'http://image.tmdb.org/t/p/w500'   trending[index] ['poster_path']))

                        )
                      ),
                      modified_text(text: trending[index]['title'], color: Colors.brown, size: 20,)
                    ],
                  ),
                ),
              );
            }),
          )
        ],

      ),

    );

  }
}

Tried doing trending[index].toString() after someone suggested so but didn't work. (The problem must be on image: and modified text... where the index is present) if there is any other way to turn index to string or as the error suggests - should be int instead of String in the first place, please reply. Or if any other reason.

CodePudding user response:

I would suggest that please create model class from json response of your Trending movies API and then create list of trending movies using that model like as below :

List<TrendingMovies> trending ;

CodePudding user response:

It is better to fetch data from API by making model class and fromJson method of that particular json response. It makes more easier to understand use data in it.

import 'package:film_library/utils/text.dart'; 
import 'package:flutter/material.dart';

class TrendingMovies extends StatelessWidget {
  const TrendingMovies({Key? key, required this.trending}) : super(key: key);
 List<TrendingMovies> trendingMoviesList ;
  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.all(10),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          const modified_text (text:'Trending Movies', size: 30, color: Colors.white,),
          SizedBox(
            height: 300,
            child: ListView.builder(
                itemCount: trending.length,
                scrollDirection: Axis.horizontal,
                itemBuilder: (context, index) {
final trendingMovie= trendingMoviesList[index];
                  return InkWell(
                    onTap: () {},
                    child: SizedBox(
                      width: 140,
                      child: Column(
                        children: [
                          Container(
                            height: 200,
                            decoration: BoxDecoration(
                              image: DecorationImage(
                                image: NetworkImage(
                                    "http://image.tmdb.org/t/p/w500${trendingMovie.poster_path}"),
                              ),
                            ),
                          ),
                          modified_text(text: trendingMovie.title, color: Colors.brown, size: 20,)
                        ],
                      ),
                    ),
                  );
                }),
          )
        ],
      ),
    );
  }
}

  • Related