NoSuchMethodError 被抛出构建选项卡(脏,状态:_TabsState#fefcf):方法 '[]' 在 null 上被调用
Posted
技术标签:
【中文标题】NoSuchMethodError 被抛出构建选项卡(脏,状态:_TabsState#fefcf):方法 \'[]\' 在 null 上被调用【英文标题】:NoSuchMethodError was thrown building Tabs(dirty, state: _TabsState#fefcf): The method '[]' was called on nullNoSuchMethodError 被抛出构建选项卡(脏,状态:_TabsState#fefcf):方法 '[]' 在 null 上被调用 【发布时间】:2021-08-09 01:07:26 【问题描述】:我已经阅读了有关此类错误的类似答案,但我仍然无法在我的代码中解决它。
当我点击按钮时,它会显示一段时间的错误屏幕,然后显示加载程序,该加载程序应在获取数据时显示。
error screen
loading screen
final screen
代码如下:
import 'package:flutter/material.dart';
import 'Dictionary.dart';
import 'Services.dart';
class NewTab extends StatefulWidget
@override
_NewTabState createState() => _NewTabState();
class _NewTabState extends State<NewTab>
List<Dictionary> _users;
int len;
@override
void initState()
super.initState();
Services.getUsers().then((users)
setState(()
_users = users;
len = users.length - 1 ;
);
);
@override
Widget build(BuildContext context)
Dictionary user = _users[len];
print(user);
return FutureBuilder(
future: Services.getUsers(),
builder: (context,AsyncSnapshot<List<Dictionary>> snapshot)
if(snapshot.data==null)
return Center(child: CircularProgressIndicator());
else
return DefaultTabController(length: 2,
child:Scaffold(
appBar: AppBar(
title : Text('Meaning',
style: TextStyle(
fontFamily: "Pangolin",
fontSize: 35,
)
),
centerTitle: true,
bottom: TabBar(
tabs: <Widget>[
Tab(text:'Meaning',
icon: Icon(Icons.book_outlined),
),
Tab(text:'Example',
icon: Icon(Icons.directions_transit)
),
],
),
),
body : TabBarView(
children: <Widget>[
Container(
color: Colors.white,
child: Container(
color: Colors.black,
child: ListView.builder(
itemCount: user.meanings.length,
itemBuilder: (context, index)
print(index);
List<Meaning> mean = user.meanings;
return ListTile(
title: Text(mean[index].definitions[0].definition
, style: TextStyle(
color: Colors.white,
),
),
);
,
),
),),Container(
child: Text('home'),
),]
)
)
);
,
);
凸起按钮中的 onpressed 功能代码
onPressed: ()=>
Navigator.of(context).push(
new MaterialPageRoute(builder: (BuildContext context) => NewTab())
)
,
services.dart 的代码
import 'package:http/http.dart' as http;
import 'Dictionary.dart';
import 'package:dictionary_app/home.dart';
import 'dart:async';
class Services
static final url = 'https://api.dictionaryapi.dev/api/v2/entries/en_US/$word';
static Future<List<Dictionary>> getUsers() async
try
print(url);
final response = await http.get(Uri.parse(url));
print(response.statusCode);
if (200 == response.statusCode)
final List<Dictionary> users = dictionaryFromJson(response.body);
return users;
else
return <Dictionary>[];
catch (e)
return <Dictionary>[];
错误:
The following NoSuchMethodError was thrown building NewTab(dirty, state: _NewTabState#1662b):
The method '[]' was called on null.
Receiver: null
Tried calling: [](null)
The relevant error-causing widget was:
NewTab file:///E:/Flutter-Projects/flutter3/dictionary_app/lib/home.dart:120:74
When the exception was thrown, this was the stack:
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:63:5)
#1 _NewTabState.build (package:dictionary_app/newtab.dart:25:30)
#2 StatefulElement.build (package:flutter/src/widgets/framework.dart:4749:27)
#3 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4632:15)
#4 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4805:11)
#5 Element.rebuild (package:flutter/src/widgets/framework.dart:4322:5)
#6 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4610:5)
#7 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4796:11)
#8 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4605:5)
... Normal element mounting (166 frames)
#174 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3662:14)
#175 MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:6285:36)
#176 Element.updateChild (package:flutter/src/widgets/framework.dart:3414:18)
#177 RenderObjectElement.updateChildren (package:flutter/src/widgets/framework.dart:5717:32)
#178 MultiChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6308:17)
#179 Element.updateChild (package:flutter/src/widgets/framework.dart:3401:15)
#180 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4657:16)
#181 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4805:11)
#182 Element.rebuild (package:flutter/src/widgets/framework.dart:4322:5)
#183 StatefulElement.update (package:flutter/src/widgets/framework.dart:4837:5)
#184 Element.updateChild (package:flutter/src/widgets/framework.dart:3401:15)
#185 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4657:16)
#186 Element.rebuild (package:flutter/src/widgets/framework.dart:4322:5)
#187 ProxyElement.update (package:flutter/src/widgets/framework.dart:4985:5)
#188 Element.updateChild (package:flutter/src/widgets/framework.dart:3401:15)
#189 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4657:16)
#190 Element.rebuild (package:flutter/src/widgets/framework.dart:4322:5)
#191 ProxyElement.update (package:flutter/src/widgets/framework.dart:4985:5)
#192 _InheritedNotifierElement.update (package:flutter/src/widgets/inherited_notifier.dart:181:11)
#193 Element.updateChild (package:flutter/src/widgets/framework.dart:3401:15)
#194 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6154:14)
#195 Element.updateChild (package:flutter/src/widgets/framework.dart:3401:15)
#196 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4657:16)
#197 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4805:11)
#198 Element.rebuild (package:flutter/src/widgets/framework.dart:4322:5)
#199 StatefulElement.update (package:flutter/src/widgets/framework.dart:4837:5)
#200 Element.updateChild (package:flutter/src/widgets/framework.dart:3401:15)
#201 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6154:14)
#202 Element.updateChild (package:flutter/src/widgets/framework.dart:3401:15)
#203 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6154:14)
#204 Element.updateChild (package:flutter/src/widgets/framework.dart:3401:15)
#205 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4657:16)
#206 Element.rebuild (package:flutter/src/widgets/framework.dart:4322:5)
#207 ProxyElement.update (package:flutter/src/widgets/framework.dart:4985:5)
#208 Element.updateChild (package:flutter/src/widgets/framework.dart:3401:15)
#209 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4657:16)
#210 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4805:11)
#211 Element.rebuild (package:flutter/src/widgets/framework.dart:4322:5)
#212 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2619:33)
#213 WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:878:21)
#214 RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:328:5)
#215 SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1147:15)
#216 SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1084:9)
#217 SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:1000:5)
#221 _invoke (dart:ui/hooks.dart:163:10)
#222 PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:259:5)
#223 _drawFrame (dart:ui/hooks.dart:126:31)
(elided 3 frames from dart:async)
【问题讨论】:
【参考方案1】:首先,您可以删除您的initState
方法,因为您已经在使用FutureBuilder
来获取您的Services.getUsers()
。
现在,您已经像这样定义了变量,
List<Dictionary> _users;
int len;
但是当第一次调用build
方法时,它们都是null
。
所以在你的构建方法里面,你不能直接使用,
Dictionary user = _users[len];
print(user);
因为第一次调用build
时_users
为空。所以删除这两行。
现在,您的其余代码都是正确的,除了一部分,
在 if(snapshot.data==null)
的 else
案例中,像这样定义 user
变量,
else
Dictionary user = snapshot.data!.last; // This gives you the last element of the array
现在,您的代码应该可以工作了。
这是paste bin link 的完整代码_NewTabState
。
【讨论】:
【参考方案2】:你必须初始化你的列表
List<Dictionary> _users = [];
更好的方法:
snapShot.connectionState == ConnectionState.waiting ? :
【讨论】:
以上是关于NoSuchMethodError 被抛出构建选项卡(脏,状态:_TabsState#fefcf):方法 '[]' 在 null 上被调用的主要内容,如果未能解决你的问题,请参考以下文章
ProviderNotFoundException 在构建 ConsumptionDialog 时被抛出,因为使用了不包含提供者的`BuildContext`
FileNotFoundException 有时会被抛出,我不知道为啥
升级颤振后颤振标签栏错误“getter'key'被称为null”