简体   繁体   English

我该如何解决这个错误? 'NoSuchMethodError' 在 null 上调用了方法 '[]'。 接收器:null。 尝试调用:[]("title")

[英]how can i solve this error? 'NoSuchMethodError' The method '[]' was called on null. Receiver: null. Tried Calling: []("title")

When i run this app, i get this mistake 'NoSuchMethodError' The method '[]' was called on null.当我运行这个应用程序时,我得到这个错误'NoSuchMethodError'方法'[]'在null上被调用。 Receiver: null.接收器:null。 Tried Calling: .尝试调用:。 I've tried different ways but it didn't worked and i didn´t found solutions on the internet, i'm new at flutter and i'm trying to learn, Do you know who to make it work?我尝试了不同的方法,但没有奏效,我没有在互联网上找到解决方案,我是 flutter 的新手,我正在努力学习,你知道谁让它工作吗? Thank you!谢谢!

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


class HomeScreen extends StatefulWidget {
  @override
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
//Step 3
  _HomeScreenState() {
    _filter.addListener(() {
      if (_filter.text.isEmpty) {
        setState(() {
          _searchText = "";
          filteredNames = titulos;
        });
      } else {
        setState(() {
          _searchText = _filter.text;
        });
      }
    });
  }

//Step 1
  final TextEditingController _filter = new TextEditingController();
  final dio = new Dio(); // for http requests
  String _searchText = "";
  List titulos = new List(); // titulos we get from API
  List filteredNames = new List(); // names filtered by search text
  Icon _searchIcon = new Icon(Icons.search);
  Widget _appBarTitle = new Text('Search Example');

  //step 2.1
  void _getNames() async {
    final response = await dio.get(
        'https://api.themoviedb.org/3/movie/popular?api_key=0e685fd77fb3d76874a3ac26e0db8a4b&language=en-US&page=1');
    print(response.data);
    List tempList = new List();
    for (int i = 0; i < response.data.length; i++) {
      tempList.add(response.data[i]);
    }
    setState(() {
      titulos = tempList;
      filteredNames = titulos;
    });
  }

//Step 2.2
  void _searchPressed() {
    setState(() {
      if (this._searchIcon.icon == Icons.search) {
        this._searchIcon = new Icon(Icons.close);
        this._appBarTitle = new TextField(
          controller: _filter,
          decoration: new InputDecoration(
              prefixIcon: new Icon(Icons.search), hintText: 'Search...'),
        );
      } else {
        this._searchIcon = new Icon(Icons.search);
        this._appBarTitle = new Text('Search a movie');
        filteredNames = titulos;
        _filter.clear();
      }
    });
  }

  //Step 4
  Widget _buildList() {
    if (!(_searchText.isEmpty)) {
      List tempList = new List();
      for (int i = 0; i < filteredNames.length; i++) {
        if (filteredNames[i]['title']
            .toLowerCase()
            .contains(_searchText.toLowerCase())) {
          tempList.add(filteredNames[i]);
        }
      }
      filteredNames = tempList;
    }
    return ListView.builder(
      itemCount: titulos == null ? 0 : filteredNames.length,
      itemBuilder: (BuildContext context, int index) {
        return new ListTile(
          title: Text(filteredNames[index]['title']),
          onTap: () => print(filteredNames[index]['title']),
        );
      },
    );
  }

  //STep6
  Widget _buildBar(BuildContext context) {
    return new AppBar(
      centerTitle: true,
      title: _appBarTitle,
      leading: new IconButton(
        icon: _searchIcon,
        onPressed: _searchPressed,
      ),
    );
  }

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: _buildBar(context),
      body: Container(
        child: _buildList(),
      ),
      resizeToAvoidBottomPadding: false,
    );
  }
}

I'm new to Dart and this is my first answer here, but maybe what you're trying to get the index of is null.我是 Dart 的新手,这是我在这里的第一个答案,但也许您要获取的索引是 null。 For example if myVariable was null, myVariable[1] or myVariable['propertyname'] can't be accessed.例如,如果myVariable为 null,则无法访问myVariable[1]myVariable['propertyname']
I think instead, you can use myVariable?['propertyname'] , which will return null if myVariable itself is null, so no errors are thrown.我认为相反,您可以使用myVariable?['propertyname'] ,如果myVariable本身是 null,它将返回null ,因此不会引发错误。 Alternatively, you could check your program flow and ensure that the variable in question is not null when you use it.或者,您可以检查您的程序流程并确保在使用时有问题的变量不是 null。

(Apologies if this was a bad answer, I'm new here) (抱歉,如果这是一个不好的答案,我是新来的)
(Please take my words with a grain of salt. I'm trying to answer the best I can but I certainly might be wrong.) (请对我的话持保留态度。我正在尽我所能回答,但我当然可能是错的。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 运行时抛出 NoSuchMethodError。 在 null 上调用了方法“[]”。 接收器:null。 尝试调用:[](0)。 如何解决这个错误? - NoSuchMethodError was thrown while running. The method '[]' was called on null. Receiver: null. Tried calling: [](0). How to solve this error? NoSuchMethodError(NoSuchMethodError:在 null 上调用了方法“[]”。接收方:null 尝试调用:[](0)) - NoSuchMethodError (NoSuchMethodError: The method '[]' was called on null. Receiver: null Tried calling: [](0)) NoSuchMethodError (NoSuchMethodError: The method &#39;[]&#39; was called on null. Receiver: null Tried calls: [](&quot;title&quot;)) - NoSuchMethodError (NoSuchMethodError: The method '[]' was called on null. Receiver: null Tried calling: [](“title”)) NoSuchMethodError: 在 null 上调用了方法“+”。 接收者:null 尝试调用:+(&quot; &quot;) - NoSuchMethodError: The method '+' was called on null. Receiver: null Tried calling: +(" ") NoSuchMethodError: 在 null 上调用了方法“[]”。 接收者:null 尝试调用:[](&quot;favorites&quot;) - NoSuchMethodError: The method '[]' was called on null. Receiver: null Tried calling: []("favorites") NoSuchMethodError:在 null 上调用了 getter 'title'。 接收器:null。 尝试调用:标题。 //有人请 - NoSuchMethodError : The getter 'title' was called on null. Receiver : null. Tried calling: title. //Anyone please 如何解决此错误:在 null 上调用了方法“[]”。 接收者:null 尝试调用:[](&quot;name&quot;) - How to solve this error: The method '[]' was called on null. Receiver: null Tried calling: []("name") 如何解决“在 null 上调用了方法 'call'。接收方:null 尝试调用:call()”在 flutter 上出现错误? - how to solve "The method 'call' was called on null. Receiver: null Tried calling: call()" error on flutter? NoSuchMethodError:在 null 上调用了方法“[]”。 接收器:null。 尝试调用:[](“名称”) - NoSuchMethodError: The method '[]' was called on null. Receiver: null. Tried calling: [](“name”) 错误:NoSuchMethodError:在 null 上调用了方法“toDouble”。 接收方:null 尝试调用:toDouble() - ERROR: NoSuchMethodError: The method 'toDouble' was called on null. Receiver: null Tried calling: toDouble()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM