我已经阅读了针对此类错误的类似答案,但仍然无法在我的代码中解决它。
当我点击按钮时,它会显示一个错误屏幕一段时间,然后显示加载器,在获取数据时应该显示该加载器。
代码如下:
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)
答案 0 :(得分:0)
首先,您可以删除您的 initState
方法,因为您已经在使用 FutureBuilder
来获取您的 Services.getUsers()
。
现在,您已经像这样定义了变量,
List<Dictionary> _users;
int len;
但是当第一次调用 null
方法时,它们都是 build
。
所以在你的build方法里面,不能直接使用,
Dictionary user = _users[len];
print(user);
因为,当第一次调用 _users
时,build
为空。所以删除那两行。
现在,除了一部分之外,其余代码都是正确的,
在 else
的 if(snapshot.data==null)
案例中,像这样定义 user
变量,
else {
Dictionary user = snapshot.data!.last; // This gives you the last element of the array
现在,您的代码应该可以工作了。
这是 _NewTabState
的完整代码的 paste bin link。
答案 1 :(得分:-1)
你必须初始化你的列表
List<Dictionary> _users = [];
更好的方法:
snapShot.connectionState == ConnectionState.waiting ? :