我正在尝试从json文件制作Listview。但是我每次都会收到此错误。我已经尝试了所有可能的解决方案,但无法使错误消失。请帮我解决问题。
我是Flutter的初学者
我的代码:
import 'dart:convert';
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:practising/screens/details.dart';
class Home extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return HomeWidgets();
}
}
class HomeWidgets extends State<Home> {
Future<List<User>> loadJson() async {
var jsonString = await rootBundle.loadString('assets/jsonFile.json');
var temp = json.decode(jsonString);
List<User> users = [];
for (var u in temp) {
User user = User(u['name'], u['phone'], u['email']);
users.add(user);
}
print(users);
return users;
}
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: loadJson(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (!snapshot.hasData) {
return Container(
child: Center(
child: Text("Loading...."),
),
);
} else {
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, int index) {
return ListTile(
leading: CircleAvatar(
child: Text(snapshot.data[index].name[0]),
),
title: Text(snapshot.data.uName),
subtitle: Text(snapshot.data[index].phone),
trailing: Icon(Icons.phone),
onTap: () {
Route r = MaterialPageRoute(
builder: (context) => details(snapshot.data[index].name,
snapshot.data[index].phone));
Navigator.push(context, r);
},
);
});
}
},
);
}
}
class User {
final String name;
final String phone;
final String email;
User(this.name, this.phone, this.email);
String get uName {
return name;
}
String get uPhone {
return phone;
}
String get uEmail {
return email;
}
}
这是错误:
I/flutter (29551): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (29551): The following NoSuchMethodError was thrown building:
I/flutter (29551): Class 'List<User>' has no instance getter 'uName'.
I/flutter (29551): Receiver: Instance(length:6) of '_GrowableList'
I/flutter (29551): Tried calling: uName
I/flutter (29551): When the exception was thrown, this was the stack:
I/flutter (29551): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
I/flutter (29551): #1 HomeWidgets.build.<anonymous closure>.<anonymous closure> (package:practising/screens/home.dart:49:45)
I/flutter (29551): #2 SliverChildBuilderDelegate.build (package:flutter/src/widgets/sliver.dart:442:15)
I/flutter (29551): #3 SliverMultiBoxAdaptorElement._build.<anonymous closure> (package:flutter/src/widgets/sliver.dart:1153:67)
I/flutter (29551): #4 _HashMap.putIfAbsent (dart:collection-patch/collection_patch.dart:137:29)
I/flutter (29551): #5 SliverMultiBoxAdaptorElement._build (package:flutter/src/widgets/sliver.dart:1153:26)
I/flutter (29551): #6 SliverMultiBoxAdaptorElement.createChild.<anonymous closure> (package:flutter/src/widgets/sliver.dart:1166:55)
I/flutter (29551): #7 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2328:19)
I/flutter (29551): #8 SliverMultiBoxAdaptorElement.createChild (package:flutter/src/widgets/sliver.dart:1159:11)
I/flutter (29551): #9 RenderSliverMultiBoxAdaptor._createOrObtainChild.<anonymous closure> (package:flutter/src/rendering/sliver_multi_box_adaptor.dart:354:23)
═══════════════════════════════════════════════ ══════════════════════════════════════════════════ ═══ I / [MALI] Gralloc:[+] r_hnd(0xae4b8fa0),客户端(56),share_fd(60) D / GraphicBuffer(29551):寄存器,句柄(0xae4b8fa0)(w:540 h:960 s:544 f:0x1 u:0x000b00)
════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following NoSuchMethodError was thrown building:
Class 'List<User>' has no instance getter 'uName'.
Receiver: Instance(length:6) of '_GrowableList'
Tried calling: uName
When the exception was thrown, this was the stack:
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
#1 HomeWidgets.build.<anonymous closure>.<anonymous closure> (package:practising/screens/home.dart:49:45)
#2 SliverChildBuilderDelegate.build (package:flutter/src/widgets/sliver.dart:442:15)
#3 SliverMultiBoxAdaptorElement._build.<anonymous closure> (package:flutter/src/widgets/sliver.dart:1153:67)
#4 _HashMap.putIfAbsent (dart:collection-patch/collection_patch.dart:137:29)
...
════════════════════════════════════════════════════════════════════════════════════════════════════
I/flutter (29551): [Instance of 'User', Instance of 'User', Instance of 'User', Instance of 'User', Instance of 'User', Instance of 'User']
════════ (2) Exception caught by widgets library ═══════════════════════════════════════════════════
Class 'List<User>' has no instance getter 'uName'.
Receiver: Instance(length:6) of '_GrowableList'
Tried calling: uName
答案 0 :(得分:1)
替换此行;
title: Text(snapshot.data.uName)
使用
title: Text(snapshot.data[index].uName)