【问题标题】:Error: type 'String' is not a subtype of type 'int' of 'index'错误:类型 \'String\' 不是 \'index\' 类型 \'int\' 的子类型
【发布时间】:2022-09-27 18:06:04
【问题描述】:
(Edit:  I have 3 files
main , trending and text. Let me show them.) I am showing you 2 of them here - where the mainly important portions are.

我认为问题出在“趋势”页面中,其中趋势 [索引] 不接受为字符串。该 api 来自 tmdb,它是一个地图列表并具有“海报路径”。不知道是什么问题。

Main => 

    class _HomeState extends State<Home> {
      @override
      void initState() {
        loadmovies();
        super.initState();
      }
      List trendingmovies = [];
      List topratedmovieslist = [];
      List tvlist = [];
      final String apikey = \'974ecf335095695927b80cb92fbe6200\';
      final readaccesstoken = \'eyJhbGciOiJIUzI1NiJ9.eyJhdWQiOiI5NzRlY2YzMzUwOTU2OTU5MjdiODBjYjkyZmJlNjIwMCIsInN1YiI6IjYzMzAwZmJkYWJkYWZjMDA3Y2Q5OWRkZCIsInNjb3BlcyI6WyJhcGlfcmVhZCJdLCJ2ZXJzaW9uIjoxfQ.LZkUXKdyN5YCWJcfV6wYLVejGvYp22BzAQowBUWe5Ys\';
    
      loadmovies() async{
        final tmdb = TMDB(
          ApiKeys(apikey, readaccesstoken),
          logConfig: const ConfigLogger(
            showLogs: true,//must be true than only all other logs will be shown
            showErrorLogs: true,
          ),);
          Map result = await tmdb.v3.trending.getTrending();
          Map topratedmovies = await tmdb.v3.movies.getTopRated();
          Map tv = await tmdb.v3.tv.getTopRated();
          setState(() {
            trendingmovies =  result[\'results\'];
            topratedmovieslist =  topratedmovies[\'results\'];
            tvlist =  tv[\'results\'];
    
          });
        print(trendingmovies);
        print(topratedmovieslist);
        print(tvlist);
    
    
    
    
      }
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            centerTitle: true,
            title: const modified_text(text:\'Film Library\', color: Colors.white70, size: 20,
          ),
            backgroundColor: Colors.transparent,
          ),
          body: ListView(
            children: [
              TrendingMovies(trending: [trendingmovies],)
    
            ],
          ),
    
        );
      }
    }


Edit: This is the trending file. I have noted that initialized list doesn\'t contain poster_path like with tmdb but the main file has extracted it to a list.
  


Trending ->
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,)
                    ],
                  ),
                ),
              );
            }),
          )
        ],

      ),

    );

  }
}

编辑:也有文本文件,但它只是用作一些字体类。我创建了模型类,例如 - 列出趋势,但它也说 - 错误:类型为 \'List\' 的值不能分配给类型为 \'Trending Movies\' 的变量,并在相同的 poster_path 上显示错误.也许将其称为@jacksparrow 的语法是错误的,虽然它没有在 [index] [\'poster_path\'] 中显示错误,但它是标题之一。

  • 趋势列表看起来如何?你能举个例子吗?
  • 您需要通过jsonDecode() 转换趋势列表,但我不确定,提供完整的 sn-p 和趋势列表
  • 根据您的代码,“趋势”应该是地图列表,每个地图都应该包含“海报路径”,但这显然不是“趋势”列表的内容.

标签: flutter dart indexing


【解决方案1】:

我建议请从您的热门电影 API 的 json 响应创建模型类,然后使用该模型创建热门电影列表,如下所示:

List<TrendingMovies> trending ;

【讨论】:

    【解决方案2】:

    最好通过制作从 API 获取数据模型类来自Json该特定 json 响应的方法。它使得更容易理解其中的使用数据。

    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,)
                            ],
                          ),
                        ),
                      );
                    }),
              )
            ],
          ),
        );
      }
    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-10
      • 2019-04-24
      • 2021-03-29
      • 1970-01-01
      • 1970-01-01
      • 2020-04-22
      • 2021-08-10
      • 2021-09-08
      相关资源
      最近更新 更多